Can You Break My Code? #6 — The Prefix Sum That Overflows

Revision en1, by ChallaAjay, 2026-10-25 11:22:36

Round 6.

Today's code is supposed to be pretty straightforward. We have an array of positive integers, and we need to find the sum of all its elements.

Nothing fancy. No segment trees, no DP, no 200-line template.

Just a loop.

The code

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;

    vector<int> a(n);
    for (int &x : a) cin >> x;

    int sum = 0;
    for (int x : a)
        sum += x;

    cout << sum << '\n';
}

Looks completely fine, doesn't it?

It compiles. It runs. It gives the correct answer on small examples.

But the constraints are:

  • 1 <= n <= 200000
  • 1 <= a[i] <= 1000000000

Your mission

Find a valid input for which this code fails.

  1. Give a counterexample that makes the output incorrect.
  2. Explain exactly what goes wrong.
  3. What's the smallest change needed to fix it?
  4. Bonus: can you explain why the same bug might go unnoticed during local testing?

Try to keep the counterexample small. No need to paste 200,000 numbers when a much smaller test can expose the problem.

Let's see who catches it first.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English ChallaAjay 2026-10-25 11:22:36 1210 Initial revision (published)