Reference solution
Time: O(n) · Space: O(1)One correct implementation. Any approach that satisfies the constraints is equally valid.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
if (!(cin >> n)) return 0;
long long sum = 0;
for (int i = 0; i < n; i++) {
long long x;
cin >> x;
sum += x;
}
cout << sum << '\n';
return 0;
}