Source Code
#include <bits/stdc++.h>

using namespace std;

#define cin(vec) for(auto& i : vec) cin >> i
#define cin_2d(vec, n, m) for(int i = 0; i < n; i++) for(int j = 0; j < m && cin >> vec[i][j]; j++);
#define cout(vec) for(auto& i : vec) cout << i << " "; cout << "\n";
#define cout_2d(vec, n, m) for(int i = 0; i < n; i++, cout << "\n") for(int j = 0; j < m && cout << vec[i][j] << " "; j++);
#define cout_map(mp) for(auto& [f, s] : mp) cout << f << "  " << s << "\n";
#define Time cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " Secs" << "\n";
#define fixed(n) fixed << setprecision(n)
#define ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
#define fill(vec, value) memset(vec, value, sizeof(vec));
#define Num_of_Digits(n) ((int)log10(n) + 1)
#define mod_combine(a, b, m) (((a % m) * (b % m)) % m)
#define all(vec) vec.begin(), vec.end()
#define rall(vec) vec.rbegin(), vec.rend()
#define sz(x) int(x.size())
#define debug(x) cout << #x << ": " << (x) << "\n";
#define fi first
#define se second
#define Pair pair < int, int >
#define ll long long
#define ull unsigned long long
#define Mod  1'000'000'007
#define OO 2'000'000'000
#define EPS 1e-9
#define PI acos(-1)

void CP_IO(){
    ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
    #endif
}

int primes[100];

int maxPrime(int x){
    if(x < 2) return x;
    int l = 0;
    while((x & 1) == 0){
        x >>= 1;
        primes[l++] = 2;
    }
    for(int i = 3; i * i <= x; i += 2){
        while(x % i == 0){
            x /= i;
            primes[l++] = i;
        }
    }
    if(l == 0) return x;
    return primes[l - 1];
}

int countBits(int x){
    int bits = 0;
    while(x != 0){
        if(x&1) bits++;
        x>>=1;
    }
    return bits;
}

vector<int> a;

void Solve(){
    int n, sum = 0;
    cin >> n;
    a.reserve(1e5+10);
    for(int i = 0, x; i < n; i++){
        cin >> x;
        a.push_back(x);
    }
    a.push_back(0);
    Two:
    for(int i = 0; i < sz(a) - 1; i++) a[i] = max(a[i],a[i+1]);
    if((sz(a) & 1) == 0){
        reverse(a.begin(), a.begin() + sz(a) / 2);
        reverse(a.begin() + sz(a) / 2, a.end());
    }
    else{
        for(int i = 0; i < sz(a); i++){
            sum += a[sz(a) - i - 1];
            if(sum > n){
                a.erase(a.begin() + sz(a) - i - 1, a.end());
                break;
            }
        }
        goto Two;
    }
    Five:
    a.push_back(3);
    a.push_back(2);
    a.push_back(1);
    for(int i = 0; i < sz(a); i++){
        if(a[i] > a.back()) swap(a[i], a.back());
    }
    a[0] = 1031;
    for(int i = 0; i < sz(a); i++){
        a[i] = maxPrime(a[i]);
    }
    sort(a.rbegin(), a.rend());
    a.erase(unique(a.begin(), a.end()), a.end());
    for(int i = 0; i < sz(a); i++){
        a[i] %= 16384;
    }
    for(int i = 1; i < sz(a) - 2; i++){
        if(a[i] == 6 && a[i + 1] == 7 && a[i + 2] == 9) swap(a[i], a[i - 1]);
    }
    for(int i = 0; i < sz(a); i++){
        a[i] *= countBits(a[i]);
    }
    if(sz(a) & 1) goto Five;
    if(a.front() == 0 && a.back() == 0){
        a.clear();
        a.push_back(1);
        a.push_back(2);
        a.push_back(3);
        a.push_back(4);
        a.push_back(5);
    }
    ll finalSum = 0;
    for(int i = 0; i < sz(a); i++){
        finalSum += a[i] * (i + 1);
    }
    cout << finalSum;
}

int main(){
    CP_IO();
    int t = 1;
    //cin >> t;
    while(t--)
        Solve();
    return 0;
}
Copy
Slavery R0X
GNU G++17
2033 ms
308 KB
Time Limit Exceeded