The winner of the card game popular in Berland "Berlogging" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line "name score", where name is a player's name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to m) at the end of the game, than wins the one of them who scored at least m points first. Initially each player has 0 points. It's guaranteed that at the end of the game at least one player has a positive number of points.
Input
The first line contains an integer number n (1 ≤ n ≤ 1000), n is the number of rounds played. Then follow n lines, containing the information about the rounds in "name score" format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive.
Output
Print the name of the winner.
Examples
Input
3
mike 3
andrew 5
mike 2
Output
andrew
Input
3
andrew 3
andrew 2
mike 5
Output
andrew
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to parse the problem. It's from Codeforces: "B. Berlogging" perhaps. But the problem statement: winner determined: if only one player has max points at end, that player wins. If multiple have max points, then wins the one who scored at least m points first (where m is the maximum final points). "scored at least m points first" means: we look at the cumulative points of each player during the game (starting from 0). Among those players who end with max points m, we consider the first round at which that player's cumulative score reached >= m. The player who reached >= m earliest (i.e., smallest round index) is the winner. If there are ties? Probably not; but maybe if same round? It says "who scored at least m points first", so if multiple reach in same round? Possibly the input ensures uniqueness? Not necessarily, but we can handle: if multiple reach in same round, we still need to choose among them? The problem might guarantee that the winner is unique. Actually, many solutions exist: they first compute final scores, determine max m, then simulate again and find first player among those with final score = m who reaches >= m. That yields a unique winner. If two players reach exactly m in the same round, it would be ambiguous. But perhaps due to scoring increments, they might both reach at the same round. But the problem likely ensures that it's unique? However, maybe the rule "first" means smallest time index; if tie, maybe both? But they want one name, so it should be unique. Could it be that ties are impossible? Possibly if two players both reach m for the first time at the same round, then they both would have to have final scores >= m (they are among the max final). But if they both reached m at same round, maybe their scores at that moment are exactly m? Or one could exceed? But then later they might change. But the rule says "scored at least m points first". If two players reach >= m at same round, then we need a tiebreaker? Not defined. Usually in such problems, there is exactly one winner. But if there is a tie, perhaps choose any? But I think the input ensures uniqueness because of the dynamic. Many Codeforces participants solved it. Let's check typical solution: compute final scores; then find max; then among those with final == max, simulate from start and the first whose cumulative >= max and final == max wins. That works. If two players have final max and both reach at same time, that would imply that at that round both have cumulative >= max and final == max. But can that happen? Suppose two players both end with 5 points. Player A gets +5 in round 1; Player B gets +5 in round 1 as well? But the rounds are individual events: each round gives points to a single player. So one round is "name score". So only one player gets points in each round. So only one player's score changes at a time. So it's impossible for two different players to both cross the threshold at exactly the same round, because crossing occurs when that player's score is updated; only one player is updated per round. So if Player A reaches >= m at round i, then at round i, A's score becomes >= m. Meanwhile, other players' scores remain unchanged that round. So they cannot cross at the same round unless they were already >= m before that round (but then they would have crossed earlier). So the first time each player reaches >= m is unique to each. The earliest among those is unique because only one player's crossing can happen at the earliest round among those crossing events. But could there be a scenario where two players have final max and the earliest crossing for both is the same round because one player already had >= m before that round? But if a player already had >= m before round i, then his first crossing would have been earlier than i. So not same as others. So it's unique. So we are safe.
Thus solution: Read n, then n lines. Each line: name, score (integer). We can first compute final scores: maintain a map from name to total. Then after reading all, find maximum total: m = max values. Collect set of candidates: names with total == m.
Then we need to simulate again: we need to track cumulative scores for each candidate? Actually we need to find the first round where any candidate's cumulative score becomes >= m, and that candidate is in the candidate set. Since we only care about candidates. However, it's possible that a non-candidate reaches >= m earlier but then later drops? The rule: we only consider among candidates. So we need to find, for each candidate, the first round index i such that at that moment (after applying the round) that candidate's cumulative score >= m. Then among these candidates, pick the one with smallest i. If two have same i? Not possible as argued, but we can handle by picking the one that occurs earliest (since only one name per round, if two candidates cross at same round, that would mean same round but only one player gets points; so only that player could cross at that round; the other candidate might have already crossed earlier or crosses later. So only one crossing per round among candidates can happen. So earliest index will be unique.
Simplest: after first pass, we have candidates set. Then second pass: maintain a map from name to cumulative score again (starting from 0). For each round i from 1 to n: read name, score (or we stored them in list). Update cum[name] += score. After updating, check if cum[name] >= m and name in candidates, and if we haven't determined a winner yet, then winner = name; break. But careful: we need the first moment when any candidate reaches >= m. But it's possible that some candidate reaches >= m at some round, but then later maybe drops? The rule says "scored at least m points first" meaning the first time they achieved at least m points (even if later they drop below? But we consider the first time they reached that level. So it's about the chronological order of reaching m for the first time, regardless of later changes. So we should check when a candidate first reaches >= m. However, if we check after each update, and as soon as a candidate meets the condition, that is the first time for that candidate? Not exactly: because the candidate might have already been >= m earlier? But we haven't detected it earlier because maybe we didn't check? Actually we check each update. So if the candidate reaches >= m at some update, we will catch it the first time it happens (since we are iterating in order). But note: a candidate could be already >= m before the update if they crossed in an earlier update. Since we are checking after each update, if they crossed earlier, we would have caught it at that earlier update. So it's fine.
One nuance: Suppose a candidate's score goes: round1: +5 (becomes 5). That's >= m if m=5. But maybe m=5 is the final max, and candidate ends with 5. At round1, they reach 5. That would be the first crossing. But it's possible that later they drop below m, but they still are candidate? But if they drop below m, then final total is not m? Actually if they drop below m, then final total cannot be m (unless they later increase again). But if they drop below m and later increase back to m, final total could still be m. Example: m=5, candidate gets +10 in round1 (score=10 >=5), later -5 (score=5), final=5. That candidate's first crossing at round1 when score became 10, which is >=5. So we would catch that at round1. However, what about a candidate who never reaches >= m until the very end? But they end with m, so they must have reached at least m at some point, maybe exactly at last round? Or they could have been exactly m throughout? Starting from 0, they can't be at m without crossing. So indeed they will have a first crossing.
But what about a candidate who first reaches >= m at a time when they are not yet in candidate set? Wait candidate set is determined by final scores. They are in candidate set because final=m. So they are candidate. So we only check after each update if name in candidate set and cum[name]>=m. That's fine.
But there is a subtlety: Suppose a player not in candidate set reaches >= m earlier, but we ignore. That's fine.
Thus algorithm:
Read n.
Store rounds in list of (name, score) pairs.
Compute final totals using map.
Determine max m.
Build set candidates = { name | totals[name] == m }.
Initialize cumulative map (maybe using default 0).
Iterate over rounds list (i from 0..n-1):
name, score = rounds[i]
cumulative[name] += score
if name in candidates and cumulative[name] >= m:
winner = name
break
Output winner.
But check example:
Example1:
3
mike 3
andrew 5
mike 2
Final: mike 5, andrew 5. m=5. candidates both.
Simulate:
1: mike +3 => 3 (<5)
2: andrew +5 => 5 (>=5, andrew in candidates) => winner andrew.
Output andrew. correct.
Example2:
3
andrew 3
andrew 2
mike 5
Finals: andrew 5, mike 5. m=5.
Simulate:
1: andrew 3 (<5)
2: andrew 5 (>=5) candidate => winner andrew. Output andrew. correct.
Edge cases: if only one candidate, we output that candidate. That works because eventually they will reach >= m and we break; or if we don't break (if they never? but they must). But we could also simply output the only candidate directly after computing. But the simulation approach works.
Complexities: n <= 1000, trivial.
Now, we need to write solution in C++ (maybe). The user said "Print the name of the winner." So implement.
But note: The problem statement: "If two or more players have the maximum number of points (say, it equals to m) at the end of the game, than wins the one of them who scored at least m points first." So need to interpret "first" means earliest in time. Done.
Potential nuance: The player must have scored at least m points first among those with final max. The phrase "scored at least m points first" could be interpreted as: among those players, the one who first reached a total of at least m (not necessarily exactly m) is winner. That's what we did.
Edge case: A player might have reached >= m but then dropped and later regained? The first crossing is what counts.
Now, there is a known Codeforces problem "B. Berlogging" (CF 2A?). Actually there is "2A Winner" or something. Wait the known problem is "2A - Winner". This problem description exactly matches Codeforces 2A: "Winner". Actually Codeforces 2A: "Winner" states: "The winner of the card game popular in Berland "Berlogging" is determined according to the following rules." Yes. So it's a classic. In that problem, they sometimes require to consider that the player who first reaches the maximum final score (after all rounds) but the maximum final score is the maximum score at the end. However, some solutions compute final scores and then simulate again. That's standard.
But one nuance: In the original problem, it's possible that a player reaches the maximum final score earlier, but later another player overtakes and ends with the same max? Wait if both end with same max, the winner is the one who first reached >= that max. However, consider scenario: Player A gets +5 in round1 => 5. Player B gets +5 in round2 => 5. Both end with 5. Winner is A because first reached 5 at round1. That's fine.
But what about scenario: Player A gets +10 in round1 => 10; Player B gets +5 in round2 => 5; Player A later gets -5 in round3 => 5. So final scores: A=5, B=5. m=5. Candidates both. When did A first reach >=5? At round1 (10). B first reaches >=5 at round2 (5). So winner A. That's consistent.
But consider scenario: Player A gets +4 in round1 => 4; Player B gets +6 in round2 => 6; Player A gets +2 in round3 => 6. Final: A=6, B=6, m=6. A first reached >=6 at round3 (after adding 2 becomes 6). B first reached >=6 at round2 (6). So winner B. That's fine.
Implementation: straightforward.
Now, but there might be a twist: The problem statement says "If at the end of the game there is only one player with the maximum number of points, he is the winner." So if only one max, we don't need to simulate. But we can still simulate and it will break at some point? Not necessarily: if only one max, maybe that player might not be the first to reach >= m? Actually if only one player has max m, they are the winner regardless. But also, it's guaranteed that at least one has positive points. However, there could be a scenario where the sole max player never reached >= m? But if final is m, then at some point they must have reached m. Actually could they end with m but never had >= m? For example, if m=5, player's scores: 4 -> 5? That's reaching exactly m. So they reached m. So they reached >= m at the moment they hit exactly m or exceeded. So yes. So they will eventually reach. However, could a player who is the sole max have reached m after another non-max player reached m earlier? That doesn't matter because winner is sole max. So we can simply output the sole max name. But simulation also works because we only check candidates (only the max player). So when they first reach >= m, we break and output. So both fine. So one algorithm handles all.
Now, we need to output the name.
Let's test a few custom cases:
Case: n=1: a 10. Final: a=10, max=10, candidates {a}. Simulate: after round1, cumulative a=10 >=10, winner a. Output a.
Case: n=2: a 5, b 5. Both final=5. Sim: round1 a=5 >=5, winner a. Output a.
Case: n=2: a 3, b 5, a 2? Actually need final both equal: a gets +3 =>3, b +5 =>5, then a +2 =>5. Final a=5, b=5. Round1: a=3; round2: b=5 (>=5 and candidate) => winner b. But a final also 5, but a reached 5 only at round3? Wait we have only 2 rounds? Actually n=2 can't have a third round. So scenario: n=3 maybe: a3, b5, a2 => as earlier.
All good.
Now, complexity: n up to 1000, no issue.
Now, implement in C++:
We'll read n.
We'll use vector<pair<string, int>> rounds;
unordered_map<string, int> totals;
Loop i=0..n-1:
string name; int score;
cin >> name >> score;
rounds.push_back({name, score});
totals[name] += score;
Now compute max:
int maxScore = -1e9; // or set from totals.
for (auto& p : totals) if (p.second > maxScore) maxScore = p.second;
Now build candidate set: unordered_set candidates;
for (auto& p : totals) if (p.second == maxScore) candidates.insert(p.first);
Now simulate:
unordered_map<string, int> cum;
string winner;
for (auto& round : rounds) {
cum[round.first] += round.second;
if (candidates.count(round.first) && cum[round.first] >= maxScore) {
winner = round.first;
break;
}
}
cout << winner << endl;
Edge case: if no winner found (should not happen), but we can handle: if winner empty, maybe find from candidates? But simulation should find.
Potential nuance: The maxScore might be negative? But problem says at least one player has positive points at the end. So maxScore >0.
But one nuance: Score increments can be negative. But final max is positive.
Also, note: In original problem "2A Winner", there is a nuance: if multiple players have same final max, we must consider the first who reached that max score (the final max) at any time. But some interpretations say "the one who first reached the maximum score that the winner ended up with". That's exactly what we did.
But there is a known tricky case: A player could have reached the max final score earlier but later dropped below, and someone else also reached the same final max later, but the first person's first crossing was earlier, so they win. That matches.
Now, test with a tricky case:
Input:
4
a 10
b 5
a -5
b 5
Finals: a=5, b=10? Actually compute: a:10-5=5; b:5+5=10 => max=10, only b, winner b.
Simulate: round1 a=10 (but a not candidate? candidates only b? final max=10, b only. So no winner yet. round2 b=5; round3 a=5; round4 b=10 => candidate b, cum[b]=10 >=10 => winner b. Output b. Ok.
Another tricky:
5
a 5
b 3
a -4
b 1
a 5
Finals: a:5-4+5=6; b:3+1=4; max=6, only a. Simulate: round1 a=5 (<6); round2 b=3; round3 a=1; round4 b=4; round5 a=6 >=6 => winner a. Good.
Now, I recall a nuance: In some solutions, they compute final scores, but then they re-apply scores only to candidates and track when they reach max. But sometimes they need to consider that the player might have crossed the threshold before being considered a candidate? But they are candidate based on final score. So it's fine.
Potential bug: What if a player reaches >= maxScore at some round, but later the maxScore changes because another player's final score is higher? Wait maxScore is final max; we compute that after all rounds. So it's fixed. The candidate set includes only those who end with that final max. However, consider a scenario where a player reaches >= X (where X is the final max) at some round, but at that time the final max might be determined by a different player who ends with a higher score. But that player's final score is not X, it's higher. But if that player ends with a higher score, then maxScore is that higher number, not X. So we are using maxScore as the final maximum. So the condition cum[name] >= maxScore is about crossing the final max. But it's possible that a candidate (with final score = maxScore) reaches exactly maxScore at some round, but later someone else surpasses that? But final max is highest, so if someone surpasses maxScore, then maxScore would be higher. But if someone surpasses maxScore later, then maxScore wouldn't be the final max; final max would be higher. So that can't happen because we have computed final max from all scores. So if any player surpasses maxScore at any point, that player's final would be at least that high, so maxScore would be at least that. But if they later drop, it's possible they surpass maxScore temporarily and then drop below, and end with less than maxScore? That would mean the final max is at least that temporary high, but if they drop, the final max might be lower? Wait: if a player at some point reaches a score > final max (which is the maximum final total), then their final total must be at least that score? Not necessarily because they could drop. For example, player A: round1: +1000, round2: -1000, final=0. The final max among all players might be something else, say 500 from player B. The final max is 500. So maxScore=500. But player A reached 1000 which is >500. So the condition cum[name] >= maxScore could be true for A even though they are not a candidate (because final = 0). That's fine; we ignore because they are not in candidates. But we might also have a candidate who reaches >=500 earlier. However, consider a candidate who also reaches >=500 earlier but then later some other player surpasses 500 and then drops; that doesn't affect the candidate's final max because final max remains 500? Actually if any player surpasses 500 at any point, does that affect maxScore? No, maxScore is computed as the final totals, not the maximum during the game. So it's possible that maxScore=500 even though someone temporarily had 1000. So the crossing condition for candidates is still cum >= 500. But is it possible that a candidate first reaches >=500 at a time when their cum is exactly 500, but later they drop and then rise again to 500? That's okay.
Now, check scenario: Player A (candidate) reaches >=500 at round 5; Player B (non-candidate) reaches >=500 at round 3; the winner is A because B not candidate. That's fine.
Now, what about scenario where a candidate's first crossing of 500 happens earlier than another candidate's, but we must ensure that the candidate we pick indeed has final = maxScore. That's already enforced.
Thus algorithm is correct.
Now, coding in C++.
Potential issues:
We need to store rounds as vector of pairs.
Use unordered_map for totals and cum.
Use unordered_set for candidates.
When reading, note that score can be negative.
There can be many players, up to 1000, each name up to 32 lowercase letters.
Now, potential alternative: after computing totals, we can directly find winner without storing rounds? No, because we need to simulate order.
One corner: The maxScore might be reached by a candidate exactly at the last round, and we need to break there. In simulation, we will catch it when that round is processed. That's fine.
Now, what if there are multiple candidates, and one candidate's first crossing occurs after another candidate's first crossing, but we break as soon as we encounter any candidate meeting cum >= maxScore. That would give the earliest crossing overall, because if a candidate crosses at round i, we will catch it at round i. Since we process rounds in order, the first time any candidate satisfies condition is the earliest crossing among all candidates. So break is correct.
But is there any case where a candidate's crossing occurs at a round, but that candidate is not the one we check because we check only the name of the player who got points that round. However, a candidate's crossing could occur without being the player who gets points? No, crossing occurs when that player's score changes. Only when we update a player can their cum change. So if a player crosses, it must be in a round where they receive points. So our check after updating that player will capture it. Could a player cross due to a round that changes another player? No, because only one player's points change per round. So our check covers all crossings.
Thus correct.
Now, but what about crossing exactly at start? At start, all are 0. But max >0, so no crossing.
Now, ensure we break immediately after the first such crossing. That is what we do.
Now, confirm with sample.
All good.
Thus solution code is simple.
Now, we need to produce answer.
Let's implement in C++.
Potential pitfalls:
Using map instead of unordered_map is fine but slower but n small.
Handling negative scores: cum[name] may become negative; that's fine.
When we compute totals, we might want to use map to preserve order? No need.
int main() {
int n;
cin >> n;
vector<pair<string, int>> rounds;
unordered_map<string, int> totals;
for (int i = 0; i < n; ++i) {
string name;
int score;
cin >> name >> score;
rounds.push_back({name, score});
totals[name] += score;
}
// find max
int maxScore = -1e9;
for (auto& p : totals) {
if (p.second > maxScore) maxScore = p.second;
}
unordered_set candidates;
for (auto& p : totals) {
if (p.second == maxScore) candidates.insert(p.first);
}
unordered_map<string, int> cum;
string winner;
for (auto& round : rounds) {
cum[round.first] += round.second;
if (candidates.count(round.first) && cum[round.first] >= maxScore) {
winner = round.first;
break;
}
}
cout << winner << endl;
return 0;
}
That should work.
Now, testing with given examples.
Example1:
3
mike 3
andrew 5
mike 2
After reading: totals: mike=5, andrew=5; max=5; candidates both. Sim: round1: mike cum=3; not; round2: andrew cum=5 >=5, candidate, winner andrew; output andrew.
Example2: similar.
Now, we might want to test a case where only one candidate. Example:
1
alice 10
totals: alice=10; max=10; candidates {alice}. Sim: round1: alice cum=10 >=10 => winner alice.
Now, test case with multiple candidates but crossing at different times:
4
a 5
b 4
a -2
b 3
Totals: a=3, b=7? Wait compute: a:5-2=3; b:4+3=7; max=7, only b. So not multiple. Need both final equal.
4
a 5
b 2
a -3
b 4
Totals: a=2, b=6 => only b.
Let's try:
5
a 5
b 3
a -4
b 1
a 5
Earlier: a=6, b=4 => only a.
To get both final equal:
4
a 5
b 5
a -2
b -2? That gives a=3, b=3 -> equal 3. But need positive: ok.
Sim: round1 a=5>=3, candidate? both final=3; but a final=3, but a reached 5 earlier, which is >=3. At round1, a is candidate (since final=3). But a's cum=5>=3, so winner a. However, is it correct? Let's think: final max =3. Both a and b end with 3. Who first reached at least 3? a reached 5 at round1, which is >=3. b reached 5? Actually b gets +5 at round2? Wait: inputs: round1 a5, round2 b5, round3 a-2 -> a=3, round4 b-2 -> b=3. So after round2, b=5 which is >=3. But a reached at round1, b at round2. So winner a. That's correct.
Now, what if a reached 5 at round1, but later drops to 3; b reaches 5 at round2, drops to 3. Winner is a.
Now, our simulation: round1: a=5, a in candidates? candidates include a and b because final totals both 3. So a is candidate, cum>=3 => winner a. That's correct. So it doesn't matter that a later drops; we capture at round1.
Thus algorithm works.
Now, an important nuance: The candidate set is built from final totals. However, if a player ends with max, they are candidate. But what if a player ends with max, but they never reached >= max during the game? Is that possible? Suppose max=5, and a player's score history: 6 -> 4 -> 5? Let's examine: Starting 0. To end at 5, they could have been 6 at some point (>=5) already. If they never reached 5 until the very end, they must have gotten increments that sum to 5 at the last move. But if they got increments that sum to 5 exactly at the last move, then at that moment they reach 5, which is >=5. So they cross at that moment. So they always cross at some point, because to go from below 5 to exactly 5, they cross. Unless they are exactly at 5 from the start (impossible). So they always have a crossing.
What if they had a pattern: 4 -> 6? That crosses at the move to 6, which is >=5. So they cross.
Thus crossing always exists.
Now, consider possibility of ties in crossing? Suppose there are two candidates: A and B. A crosses at round i, B crosses at round j, i<j. Winner A. Our algorithm will capture at round i. Good.
Now, check if there is any scenario where a candidate crosses at round i but we don't detect because we only check after updating the name. Since crossing occurs only when that player is updated, we will check. Good.
Now, edge: A candidate could have been already above maxScore before we start simulation? No, they start at 0 and max>0.
Thus code is correct.
Now, let's think about potential pitfalls: when we compute totals after reading all rounds, we sum scores. But note: the totals used to determine max are final scores. However, there is a nuance: The problem's statement: "If at the end of the game there is only one player with the maximum number of points, he is the winner." This is straightforward.
But there is a known alternative interpretation: In some old problems, the winner is determined by the maximum points at any time? Actually no, this is clear: at the end, maximum points; if tie, first to reach that maximum points.
Now, there is also nuance: Some solutions first compute final totals, then simulate only for those who have final total >= max? But they use condition cumulative >= max and final total == max. That's same.
Now, I think we are good.
One more nuance: In the problem, it's said "scored at least m points first". Does "first" mean among those players? Or "first" in time overall? It says "than wins the one of them who scored at least m points first." So among them, the one who first scored at least m. Yes.
Now, let's implement.
But maybe we need to consider that the player who first reaches >= m might not have final m if they later drop? But we restrict to those with final m. So it's fine.
Now, test with a tricky case found in CF editorial:
Input:
3
a 1
b 2
a 2
Final totals: a=3, b=2, max=3, only a winner. Sim: round1 a=1; round2 b=2; round3 a=3 >=3 => winner a. Works.
Another tricky:
4
a 5
b 5
a -2
b -2
Final: a=3, b=3, max=3. Candidates both. Sim: round1 a=5 >=3 => winner a. But some might argue that a's 5 is not exactly m (3) but it's at least m. So winner a. That matches.
But wait: Does the rule "scored at least m points first" refer to scoring at least m points during the game? Yes.
Now, consider scenario where both a and b reach exactly m at the same round? That can't happen because only one player per round. But consider they both could have reached m before that round, but then the first crossing times differ. So not same.
Thus done.
Now, I'll produce final answer with explanation.
But since the user asked to "Print the name of the winner." and gave examples, they likely expect code. However, as ChatGPT, I need to provide solution. Possibly they want the solution in a specific language? The problem statement didn't specify, but typical for Codeforces is C++ (or any). We'll provide C++ code.
We'll also provide explanation? The instruction just says "Print the name of the winner." That's output of program. But likely they want the solution code. So we produce code.
But wait, the prompt might be a request to solve the problem, i.e., to output the name of the winner given input? No, it says: "The first line contains an integer number n... Input ... Output Print the name of the winner." That's the problem statement. Then they gave examples. This is typical of competitive programming problem statements. The user likely wants the solution code in a specific language? They didn't specify. However, often in such conversations, the user expects an explanation and perhaps the code. I'll provide a thorough explanation and then the code.
Given the typical ChatGPT format: they ask "Can you solve this problem?" and then provide statement. So we should output solution.
Given the instructions: "You are an AI. Respond." I think we should output final answer with code.
But to be safe, I'll output explanation and then code.
But the user may only want the code. However, ChatGPT in analysis mode often provides reasoning then final answer. I'll do that.
Thus final answer: provide C++ solution.
But also note that there may be alternative languages: Python, but C++ is typical.
Given the constraints, Python is fine too. But I'll provide C++.
I'll output final code.
Now, check for possible errors:
In C++, unordered_map does not have default 0 for int? Actually accessing with [] creates entry with default 0. So cum[round.first] += round.second works. Even if cum didn't have that key, it creates with value 0 then adds. That's fine.
Similarly totals.
The break after setting winner ensures we stop early.
Ensure we include necessary headers: iostream, vector, string, unordered_map, unordered_set.
Now, note: The input may have extra spaces; using cin >> name >> score handles that.
Now, test with maximum n: 1000, each name up to 32 chars, fine.
Now, any need to use long long? Score range per round: -1000 to 1000, n up to 1000, total sum per player up to +/-1e6, fits in int (32-bit). So int is fine.
Now, final output: cout << winner << endl;
Now, we could also handle scenario where winner not found (should not). But for safety, we could after loop, if winner empty, then just pick any from candidates? But not needed.
Now, some test: Suppose final max is 0? But problem says at least one player has positive points at end, so max >0.
Thus code ready.
Now, also note: The problem's name "Berlogging" maybe typo, but it's fine.
vector<pair<string, int>> rounds;
unordered_map<string, int> total;
for (int i = 0; i < n; ++i) {
string name;
int score;
cin >> name >> score;
rounds.push_back({name, score});
total[name] += score;
}
// Find the maximum final score
int maxScore = -1e9;
for (auto& p : total) {
if (p.second > maxScore) maxScore = p.second;
}
// Collect all players with that maximum
unordered_set<string> candidates;
for (auto& p : total) {
if (p.second == maxScore) candidates.insert(p.first);
}
// Simulate again and find the first who reaches >= maxScore
unordered_map<string, int> current;
string winner;
for (auto& round : rounds) {
current[round.first] += round.second;
if (candidates.count(round.first) && current[round.first] >= maxScore) {
winner = round.first;
break;
}
}
cout << winner << endl;
return 0;