# get the array size n=int(raw_input()) # get the elements of the array space seperated as a string s=raw_input() # add the elements into the list after conversion to integer list=map(int,s.split(' ')) # print the sum of elements in the list print sum(list)
C Implementation:
#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> typedef unsigned long long int ll; int main(){ ll n,x,sum = 0; scanf("%llu",&n); while(n--) { scanf("%llu",&x); sum += x; } printf("%llu",sum); return 0; }
Java Implementation:
import java.util.*; import java.lang.*; import java.io.*; class CodeRegister { public static void main (String[] args) throws java.lang.Exception { Scanner in = new Scanner(System.in); long n = in.nextInt(); long sum = 0; while(n>0) { long x = in.nextInt(); sum += x; n--; } System.out.print(sum); } }