atcoder#ABC261D. [ABC261D] Flipping and Bonus
[ABC261D] Flipping and Bonus
Score : points
Problem Statement
Takahashi will toss a coin times. He also has a counter, which initially shows .
Depending on the result of the -th coin toss, the following happens:
- If it heads: Takahashi increases the counter's value by and receives yen (Japanese currency).
- If it tails: he resets the counter's value to , without receiving money.
Additionally, there are kinds of streak bonuses. The -th kind of streak bonus awards yen each time the counter shows .
Find the maximum amount of money that Takahashi can receive.
Constraints
- are all different.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the maximum amount of money that Takahashi can receive, as an integer.
6 3
2 7 1 8 2 8
2 10
3 1
5 5
48
If he gets head, head, tail, head, head, head, in this order, the following amounts of money are awarded.
- In the -st coin toss, the coin heads. Change the counter's value from to and receive yen.
- In the -nd coin toss, the coin heads. Change the counter's value from to and receive yen. Additionally, get yen as a streak bonus.
- In the -rd coin toss, the coin tails. Change the counter's value from to .
- In the -th coin toss, the coin heads. Change the counter's value from to and receive yen.
- In the -th coin toss, the coin heads. Change the counter's value from to and receive yen. Additionally, get yen as a streak bonus.
- In the -th coin toss, the coin heads. Change the counter's value from to and receive yen. Additionally, get yen as a streak bonus.
In this case, Takahashi receives yen in total, which is the maximum possible. Note that streak bonuses are awarded any number of times each time the counter shows . As a side note, if he gets head in all coin tosses, he only receives yen, which is not the maximum.
3 2
1000000000 1000000000 1000000000
1 1000000000
3 1000000000
5000000000
Note that the answer may not fit into a -bit integer type.