B - 料理コンテスト / Cooking Contest Editorial by admin
GPT 5.2 HighOverview
This is a problem where you calculate each participant’s final score \(A_i + B_i\) and find the participant number with the maximum value.
Analysis
- Since the final score is simply \(A_i + B_i\), you just need to compute the total score for everyone and find the “maximum value.”
- It is guaranteed that “there is exactly one person with the highest score,” so there is no need to handle ties (dealing with multiple candidates).
- You could naively store all \(A_i, B_i\) in arrays and then find the maximum in \(O(N)\), but updating the maximum while reading without storing is simpler and saves memory.
- Since \(N \le 2 \times 10^5\), processing one iteration at a time in a loop is the most reliable approach (brute force is sufficiently fast).
Example:
- Participant 1: \(A_1=30, B_1=40 \Rightarrow 70\)
- Participant 2: \(A_2=50, B_2=10 \Rightarrow 60\)
- Participant 3: \(A_3=20, B_3=80 \Rightarrow 100\)
The maximum is 100, so the answer is 3.
Algorithm
- Prepare variables
best_sum(the maximum total score so far) andbest_idx(the corresponding participant number). - For \(i=1\) to \(N\), read \((A_i, B_i)\) in order and compute \(s = A_i + B_i\).
- If \(s > best_sum\), update
best_sum = sandbest_idx = i. - After processing all participants, output
best_idx.
Complexity
- Time complexity: \(O(N)\) (each participant is processed exactly once)
- Space complexity: \(O(1)\) (only the maximum value and its index are stored; the entire input is not saved in an array)
Implementation Notes
Participant numbers are 1-indexed as stated in the problem, so writing the loop as
for i in range(1, n + 1)helps avoid off-by-one errors.In Python, when the input is large, reading all at once with
sys.stdin.buffer.read().split()is faster than repeatedly callinginput()(the submitted code uses this approach).Source Code
import sys
def main():
it = iter(sys.stdin.buffer.read().split())
n = int(next(it))
best_sum = -1
best_idx = -1
for i in range(1, n + 1):
a = int(next(it))
b = int(next(it))
s = a + b
if s > best_sum:
best_sum = s
best_idx = i
print(best_idx)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
posted:
last update: