Source Code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int N = 1000000;
int largest[2000000];
int p[2000000];
int main() {
    ios_base::sync_with_stdio(0);
    for (int i=2;i<=N;i++) {
        if (p[i])continue;
        largest[i]=i;
        for (int j=2*i;j<=N;j+=i) {
            p[j] = 1;
            largest[j] = i;
        }
    }
    int n;
    cin>>n;
    vector<ll> v;
    largest[1]=1;
    for (int i=0;i<n;i++) {
        int x;
        cin>>x;
        v.push_back(x);
    }
    // 1
    v.push_back(0);
    // 2
    for (int i=0;i+1<v.size();i++) {
        v[i] = max(v[i], v[i+1]);
    }
    //3-4
    while(1){
        if (v.size()%2) {
            int cur = 0;
            while(1) {
                if (v.size()==0) break;
                int c = v.back();
                if (cur+c > n)break;
                cur+=c;
                v.pop_back();
            }
        } else break;
    }
    vector<int> a, b;
    for (int i=0;i<v.size();i++) {
        if (i<v.size()/2) a.push_back(v[i]);
        else b.push_back(v[i]);
    }
    if (v.size()){
        v.clear();
        reverse(a.begin(), a.end());
        reverse(b.begin(), b.end());
        for (auto x:a) v.push_back(x);
        for (auto x:b) v.push_back(x);
    }
    while(1) {
        // 5
        v.push_back(3);
        v.push_back(2);
        v.push_back(1);
        // 6
        for (int i=0;i<v.size();i++) if (v[i]>v[v.size()-1])swap(v[i], v[v.size()-1]);
        // 7
        v[0]=1031;
        // 8
        for (int i=0;i<v.size();i++) v[i] = largest[v[i]];
        // 9
        sort(v.begin(),v.end());
        reverse(v.begin(), v.end());
        // 10
        for (int i=0;i<v.size();i++) v[i] = v[i] % 16384;
        // 11
        for (int i=1;i+2<v.size();i++) {
            if (v[i]==6 && v[i+1]==7 && v[i+2]==9) swap(v[i], v[i-1]);
        }
        // 12
        for (int i=0;i<v.size();i++) {
            v[i] = __builtin_popcount(v[i]) * v[i];
        }
        // 13
        if (v.size()%2==0) break;
    }
    // 14
    if (v[0] == 0 || v[v.size()-1]==0) {
        cout<<55<<endl;
        return 0;
    }
    // 15
    ll ret = 0;
    for (ll i=1;i<=v.size();i++) {
        ret += i*(v[i-1]);
    }
    cout<<ret<<endl;
}
Copy
Slavery RedNextCentury
GNU G++17
78 ms
10.4 MB
Accepted