Source Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
int ans[25];
void add(ll x){
    int digit = 0;
    int carry = 0;
    while(x){
        ans[digit] += x%10 + carry, carry = 0;
        x/=10;
        if(ans[digit] > 9)
            carry = 1, ans[digit] -= 10;
        // cout<<ans[digit]<<" ";
        digit++;
    }
    if(carry)
        ans[digit] = 1;
}

void solve(){
    int n;
    cin>>n;
    ll a[n];
    for(int i = 0 ; i<n ; i++){
        cin>>a[i];
        add(a[i]);
    }
    int sum = 0;
    for(int i = 0 ; i<25 ; i++){
        sum+=ans[i];
    }
    while(sum>9){
        int temp = sum;
        int sum2 = 0;
        while(temp){
            sum2 += temp%10;
            temp/=10;
        }
        sum = sum2;
    }
    cout<<sum;
}
 
int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.in", "r", stdin);
    #endif
 
    int tt=1;//cin>>tt;
    while(tt--)
        solve();
}
Copy
The Tale of a Generous Prince hashemtb
GNU G++17
19 ms
1.8 MB
Wrong Answer