Source Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;


string add(string a, string b){
    string result = "";
    int carry = 0;
    int i = a.size()-1, j = b.size()-1;
    while(i>=0 || j>=0){
        int sum = carry;
        sum += (i>=0) ? a[i] - '0' : 0;
        sum += (j>=0) ? b[j] - '0' : 0;
        result = to_string(sum%10) + result;
        carry = sum/10;
        i--,j--;
    }
    if(carry) 
        result = to_string(carry) + result;
    return result;
}

void solve(){
    int n;
    cin>>n;
    string sum = "";
    for(int i = 0 ; i<n ; i++){
        string b;
        cin>>b;
        sum = add(sum, b);
    }
    int ans = 0;
    for(int i = 0 ; i<sum.size() ; i++){
        ans += sum[i] - '0';
    }
    while(ans > 10){
        string s = to_string(ans);
        ans = 0;
        for(int i = 0 ; i<s.size() ; i++){
            ans+= s[i] - '0';
        }
    }
    cout<<ans<<endl;
}


int main(){
    #ifndef ONLINE_JUDGE
        freopen("input.in", "r", stdin);
    #endif


    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int t = 1;
    // cin>>t;
    while(t--)
        solve();

}
Copy
The Tale of a Generous Prince YouKn0wWho
GNU G++17
105 ms
1.1 MB
Wrong Answer