Source Code
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <iomanip>
using namespace std;

typedef long long ll;
#define forn(i, n) for (ll i = 0; i < ll(n); i++)
#define cin(a,n) forn(i,n) cin >> a[i]
#define cout(a,n) forn(i,n) cout << a[i] << ' '
#define FastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define all(a) a.begin(), a.end()
#define alln(a, n) a, a + n
#define rall(a) a.rbegin(), a.rend()
#define ralln(a) a,a+n, greater<int>()
#define yes "YES"
#define no "NO"
#define yn(b) cout<<(b?yes:no)<<'\n' 
#define pb push_back

ll kage(ll SUM) {
	if (SUM < 10) return SUM;
	ll x = 0;
	while (SUM > 0) {
		x += (SUM % 10);
		SUM /= 10;
	}
	kage(x);
}

void solve() {
	ll SUM = 0;
	int n;
	cin >> n;
	forn(i, n) {
		ll a;
		cin >> a;
		SUM += a;
	}
	cout << kage(SUM) << '\n';

}

int main(void) {
	FastIO;
	int t = 1;
	//cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}
Copy
The Tale of a Generous Prince misterRoshi22
GNU G++17
2094 ms
204.5 MB
Time Limit Exceeded