The two branch circuit breakers will trip if the load on their protected circuit is greater than 32 amperes, and 25 amperes respectively. However, since the main breaker trips at 40 amperes. If both branch circuits are pulling a full load the branch circuit breakers will not trip, but the combination of the loads will trip the main breaker (32 + 25 = 57 > 40 as you have said).
It's a very common situation to have the branch circuit breakers total more than the main breaker, but this is almost never a problem since the circuits usually don't pull a full load. Remember, circuit breakers are there to protect the wires. They shut off the power if you are drawing enough power to damage the wire, so installing a larger main breaker is likely not an option (unless you contact your power company and have the service upgraded, which will include upgrading the feeder cable).
Contact your local power company, and speak to them about a service upgrade. Other than plugging less stuff in, or cycling loads, there is not much you can do yourself in this situation.
The only "calculation" is "how many circuits are expected to draw how much, at the same time" - and if it's more than 200 amps, you'll need a bigger main, or two 200A panels, or something like that.
It's quite normal to have far more than 200A of breakers attached to a panel with a 200A main. Whether that ever becomes a problem of tripping the main is a matter of use patterns. As an example, it's rare for an electric range to have all burners and the oven on full power, at the same time.
Likewise, many 15 and 20 amp lighting and outlet circuits are rarely fully loaded.
Breakers (and wire) for "continuous" circuits are derated, so they only draw (at most) 80% of the rated breaker amps (breaker is sized for 125% of load - the math works out) if correctly sized.
Most appliances that get a dedicated circuit call for a breaker considerably larger than the actual running current. Starting currents may be much higher, but are rarely if ever applied to all circuits at the same time.
On the other hand, if you had 10 20-ampere electric resistance heating circuits (drawing 16 amps per derating) that would be all you could put on one 200 amp main (as the entire load is continuous) other than some load that would not turn on at the same time (say a big honking air conditioner, if the control systems are sane and don't run heat and A/C at the same time).
Best Answer
The main breaker is sized to trip before the total current draw of the panel exceeds either a) the current carrying capacity of the feed from the electric company, or b) the current carrying capacity of the breaker panel bus bar, whichever is less. So if you order a 100 amp service and your breaker panel is rated for 120 amps, then your main panel should have a 100-amp breaker at the top.
The breakers for the branch circuits are rated to prevent the branch circuit from drawing more power than the branch circuit wiring can handle.
If you look at your panel and do the math, it is very possible that the branch breakers have a total capacity greater than the rating of the main breaker. This is fine because you will typically never have everything drawing it's maximum rated current.