#B3781. Surprise Letter Simulation
Surprise Letter Simulation
Surprise Letter Simulation
In this problem, each letter consists of an envelope and a sheet of paper, both of which have weight. The weight per square centimeter of the envelope is \( x \) mg and that of the paper is \( y \) mg. For a letter with envelope area \( S \) (in cm2) and paper area \( s \) (in cm2), the weight of the envelope and the paper are \( S\times x \) mg and \( s\times y \) mg respectively. The letter might also contain a gift with a positive weight. In particular, the total weight of the letter is given as \( M \) mg (an integer) so that the gift's weight (if any) is defined as:
[ M' = M - (S\times x + s\times y). ]
Note that if \( M' = 0 \) then the letter does not contain any gift.
Letters are opened one by one and a "surprise value" is updated according to the following rules (initial surprise value is 0):
- No Gift: If a letter does not contain a gift (i.e. \( M' = 0 \)), the surprise value remains unchanged. However, if there are at least \( b \) consecutive letters without a gift, then starting from the \( b\text{-th} \) such letter, for every subsequent letter without a gift the current surprise value is halved and rounded down (i.e. floor division) until a letter with a gift appears. (If the current surprise value is 0, halving does not change it.)
- Gift: If a letter contains a gift (i.e. \( M' > 0 \)) with weight \( M' \) mg, the surprise value increases by \( M' \). Moreover, if \( M' \) is strictly greater than the total mass of the envelope and paper (i.e. if \( M' > S\times x + s\times y \)), an extra bonus of \( 0.5\,M' \) is added (after which the bonus is rounded up using the ceiling function). Furthermore, if there are at least \( a \) consecutive letters with a gift, then starting from the \( a\text{-th} \) such letter, after calculating the basic addition and bonus for that letter, the entire current surprise value is multiplied by 2; this doubling continues for every subsequent letter with a gift until a no-gift letter is encountered.
Your task is to process the letters in order and output two numbers: the maximum surprise value reached at any moment during the process, and the final surprise value after all letters have been processed.
inputFormat
The first line contains five space-separated integers: \( n \) (the number of letters), \( a \) (the threshold for consecutive gift letters), \( b \) (the threshold for consecutive no-gift letters), \( x \) and \( y \) (the weight per square centimeter for the envelope and paper respectively).
Each of the next \( n \) lines contains three space-separated integers: \( S \) (envelope area in cm2), \( s \) (paper area in cm2), and \( M \) (total mass in mg).
outputFormat
Output two integers separated by a space: the maximum surprise value encountered and the final surprise value after processing all letters.
sample
3 2 2 1 1
10 10 20
10 10 20
10 10 20
0 0