Since n \le 10^5 and each number does not exceed 10^9 in absolute value, the sum of the given numbers can be up to approximately 10^{14}. To compute the result, use the long long type.
Algorithm implementation
Read the input data until the end of the file. Sum up the given numbers.
res = 0;
while(scanf("%lld",&n) == 1)
res += n;Print the result.
printf("%lld\n",res);Java implementation
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
long sum = 0;
while(con.hasNext())
{
long val = con.nextLong();
sum += val;
}
System.out.println(sum);
con.close();
}
}Python implementation
import sysRead the input data until the end of the file. Sum up the given numbers.
sum = 0
for line in sys.stdin:
for var in line.split():
sum = sum + int(var)Print the result.
print(sum)Python implementation — read from file
sum = 0
with open('d:\\WorkPython\\520.in', 'r') as file:
for line in file:
for var in line.split():
sum += int(var)
print(sum)