Accel Thinking

CP Weekly Challenges

CCC 2017 S1 Sum Game Solution: Running Prefix Sums and Last Equal Day

Learn how to solve Sum Game with running prefix sums, cumulative totals, and last-valid-position tracking in Python and C++.

This problem is about two baseball teams, but the algorithmic idea is a classic running prefix sum. We compare the two teams’ cumulative scores after each day and remember the last day when those totals are equal.

The important detail is that we are not comparing the scores from a single day. We are comparing the total scores from day 1 through day K.

Where This Fits in the CCC DSA Map

What to Study Next

What the Problem Is Asking

The season lasts for N days. The Swifts and the Semaphores each play one game per day.

The input gives:

N
daily scores for the Swifts
daily scores for the Semaphores

We need to output the largest integer K such that after K days:

Swifts total score == Semaphores total score

If there is no such day after the season starts, we output 0.

The teams are tied before any games are played, but that does not count as a positive K.

Where Beginners Often Get Stuck

The first common mistake is comparing daily scores instead of cumulative scores. The problem asks whether the two teams have the same total after K days.

The second common mistake is stopping at the first equal total. The problem asks for the largest K, so we must scan all days and keep updating the answer.

The third common mistake is forgetting that the answer can be 0.

Core Algorithm Idea

Maintain two running totals:

swift_total
semaphore_total

For each day, add both teams’ scores to their totals. If the totals are equal, record the current day as the answer.

Because we scan days from left to right, the last recorded equal day is automatically the largest valid K.

Python Solution

N = int(input())
swift_scores = list(map(int, input().split()))
semaphore_scores = list(map(int, input().split()))

swift_total = 0
semaphore_total = 0
answer = 0

for i in range(N):
    swift_total += swift_scores[i]
    semaphore_total += semaphore_scores[i]

    if swift_total == semaphore_total:
        answer = i + 1

print(answer)

C++ Solution

#include <iostream>
#include <vector>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    cin >> N;

    vector<int> swift(N);
    vector<int> semaphore(N);

    for (int i = 0; i < N; i++) {
        cin >> swift[i];
    }

    for (int i = 0; i < N; i++) {
        cin >> semaphore[i];
    }

    int swiftTotal = 0;
    int semaphoreTotal = 0;
    int answer = 0;

    for (int i = 0; i < N; i++) {
        swiftTotal += swift[i];
        semaphoreTotal += semaphore[i];

        if (swiftTotal == semaphoreTotal) {
            answer = i + 1;
        }
    }

    cout << answer << "\n";
    return 0;
}

Complexity Analysis

We scan the N days once.

The time complexity is:

O(N)

The shown implementation stores the two score arrays, so the memory complexity is:

O(N)

The idea can also be implemented with O(1) extra memory if the input is streamed or processed in a different format.

Common Mistakes

  1. Comparing daily scores instead of cumulative scores.
  2. Stopping at the first equal cumulative total.
  3. Forgetting that the answer can be 0 .
  4. Using i instead of i + 1 for the day number.
  5. Mixing up prefix-array indices with day numbers.

Review Questions

  1. Why do we need cumulative totals in this problem?
  2. Why should we continue scanning after finding an equal day?
  3. Why is answer initialized to 0 ?
  4. Why does i + 1 represent the current day?
  5. When would a full prefix sum array be more useful than two running totals?