Source Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
int ans[25];
void add(string num){
    // add the number to the answer manual addition
    int i = 0, carry = 0;
    while(i < num.size()){
        ans[i] += (num[i] - '0') + carry;
        carry = ans[i] / 10;
        ans[i] %= 10;
        i++;
    }
    while(carry){
        ans[i] += carry;
        carry = ans[i] / 10;
        ans[i] %= 10;
        i++;
    }
}

void solve(){
    int n;
    cin>>n;
    string 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
25 ms
8.7 MB
Accepted