Source Code
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    int n,a;
    cin>>n;
    int sum = 0;
    for(int i = 0 ; i < n ; i++)
    {
        cin>>a;
        sum+=a;
    }
    while(!(sum<=9 && sum>=0))
    {
        vector <int> d;
         int temp = sum;
         int z = 0;
        while(temp)
        {
            d.push_back(temp%10);
            temp = temp/10;
        }
        for(int i = 0; i <d.size(); i++)
        {
            z+=d[i];       
        }
        sum = z;
    }
    cout<<sum;
   

    return 0;
}
Copy
The Tale of a Generous Prince Abushukur
GNU G++17
2091 ms
768 KB
Time Limit Exceeded