{ }codelper
64-bit integersoverflowsumP0015

Big sum

The first line gives the count n, and the second line gives n integers separated by spaces.
Print their sum.

Input
1 ≤ n ≤ 10,000, -1,000,000,000 ≤ each number ≤ 1,000,000,000 (each number fits in a 32-bit int)
Output
the sum on one line (between -10^13 and 10^13)
Example 1 · stdin
3
1000000000 1000000000 1000000000
stdout
3000000000
Example 2 · stdin
4
5 -2 7 -10
stdout
0
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

Each number fits in an int (32-bit, about ±2.1 billion), but the sum does not.
So reading the numbers as int is fine, but the variable that collects the sum must be 64-bit: C/C++ long long (C's long is 32-bit on Windows), Java/C# long.
With int you get a wrong number and no error — Java and C# wrap silently by default (in C# wrapping it in checked throws), and in C/C++ signed integer overflow is undefined behavior.
Python integers have no size limit, and JS/TS/Craykon have a single double-precision number type that is exact up to 2^53 (about 9×10^15), so this problem (at most 10^13) just works.

Pythonmain.py · 2 lines
n = int(input())print(sum(map(int, input().split())))
JavaScriptmain.js · 3 lines
const [n, ...a] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);// 합은 최대 10^14 — 2^53(약 9×10^15)보다 작아서 Number로 정확하다console.log(a.reduce((s, x) => s + x, 0));
TypeScriptmain.ts · 3 lines
import { readFileSync } from "node:fs";const [n, ...a]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);console.log(a.reduce((s, x) => s + x, 0));
JavaMain.java · 14 lines
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.StringTokenizer; public class Main {    public static void main(String[] args) throws Exception {        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        int n = Integer.parseInt(br.readLine().trim());        StringTokenizer st = new StringTokenizer(br.readLine());        long total = 0; // 수 하나는 int에 들어가도 합은 long이 필요하다        for (int i = 0; i < n; i++) total += Integer.parseInt(st.nextToken());        System.out.println(total);    }}
C#Main.cs · 13 lines
using System; class Program{    static void Main()    {        Console.ReadLine();        long total = 0; // int로 두면 조용히 넘친다(C#은 기본이 unchecked)        foreach (string s in Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries))            total += int.Parse(s);        Console.WriteLine(total);    }}
C++main.cpp · 14 lines
#include <iostream> int main() {    std::ios::sync_with_stdio(false);    std::cin.tie(nullptr);    int n, x;    long long total = 0;    std::cin >> n;    for (int i = 0; i < n; i++) {        std::cin >> x;        total += x;    }    std::cout << total << '\n';}
Cmain.c · 13 lines
#include <stdio.h> int main(void) {    int n, x;    long long total = 0; /* long은 Windows에서 32비트라 long long을 쓴다 */    scanf("%d", &n);    for (int i = 0; i < n; i++) {        scanf("%d", &x);        total += x;    }    printf("%lld\n", total);    return 0;}
Craykonmain.cray · 8 lines
n = Console.ReadLine().ToNumber();수들 = Console.ReadNumbers();합 = 0;foreach (x in 수들){    합 += x;}Console.WriteLine(합);