The logic is that you can't win more against any other player than you bet, and when you fold you forfeit all bets you have made into the pot. For any given pot the best (or last, if everyone else has folded) hand that has an interest in that pot (has money in it and hasn't folded) wins that pot. Your amounts of 120 and 200 are correct. To break it down:
Player A all in for 40.
Player B raises to 80. Main pot 40(A)+40(B), side pot has 40(B).
Player C raises to 200. Main pot 40(A)+40(B)+40(C), side pot has 40(B)+160(C).
Player B folds, forfeiting side pot of 200 to B. (A does not have an interest in this side pot.)
Player A wins at showdown, and gets main pot of 120.