Hua-Hua often sees an advertisement for a mobile game while browsing social media apps. In the game, players control a character who continuously challenges monsters. If the player's character has a higher combat power than the monster, they can defeat the monster and absorb its combat power value; otherwise, if the player's character has the same or lower combat power than the monster, the player's character will die, and the game will end.
Please write a program that calculates the player character's combat power at the end of the game, given the combat power values of the encountered monsters in sequence.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The first line of input contains two integers, M (1 ≤ M ≤ 300) and A (1 ≤ A ≤ 10000), representing the number of monsters and the initial combat power of the player, respectively. The second line contains M integers Ti (1 ≤ Ti ≤ 6000, i = 1, 2, 3, …, M), representing the combat power values of the monsters that the player will encounter in sequence. | Please output the combat power of Hua-Hua when the game ends. |
5 20 30 60 90 120 150 | 20 |
4 32 31 61 91 215 | 215 |
3 10000 10 20 30 | 10060 |
Thought Process
Use a boolean value to determine whether combat power needs to be evaluated, initially set to True. If encountering a monster with the same or higher level, set the boolean value to False and do not evaluate combat power when collecting data thereafter.
Sample Code-ZeroJudge J178: Mobile Game
#include <iostream>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int M, A;
cin >> M >> A;
bool stop = false;
for (int i = 0; i<M; i++)
{
int tmp;
cin >> tmp;
if (A > tmp && !stop) A += tmp;
else stop = true;
}
cout << A << "\n";
}
//ZeroJudge J178
//Dr. SeanXD