Source Code
/// Zengy Manga
#pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;

#include "bits/stdc++.h"
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace __gnu_pbds;
using namespace __gnu_cxx;

using ll = long long;
using ii = pair<int, int>;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define pb push_back
#define F first
#define S second
#define f(i, a, b)  for(int i = a; i < b; i++)
#define all(a)  a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define mp make_pair
#define popCnt(x) (__builtin_popcountll(x))
#define int ll

const int N = 1e6+5, LG = log2(N) + 1, MOD = 1e9 + 7;
const double PI = acos(-1);
int pr[300005];
void doWork() {
    f(i,2,300005)if(!pr[i])for(int j = i; j <= 300000; j+=i)pr[j]=i;
    pr[1]=1;
    int n;
    cin >> n;
    vector<int> a(n);
    f(i,0,n)    cin >> a[i];
    a.pb(0);
    f(i,0,n)a[i]=max(a[i],a[i+1]);
    while(true) {
//        for(int i = n-1;i>=0;--i)a[i]=max(a[i],a[i+1]);
        if(a.size()%2==0) {
            reverse(a.begin(),a.begin()+a.size()/2);
            reverse(a.begin()+a.size()/2,a.end());
            break;
        }   else {
            int curSum=0;
            while(a.size()) {
                curSum+=a.back();
                if(curSum>n)break;
                a.pop_back();
            }
        }
    }
    while(true) {
    a.push_back(3);
    a.push_back(2);
    a.push_back(1);

    for(auto & x : a)
        if(x > a.back())swap(x,a.back());
    a[0]=1031;
    for(auto & x : a)x=pr[x];

    sort(rall(a));

    for(auto & x : a)
        x %= 16384;
    for(int i = 1; i + 3 <= a.size(); i++) {
        if(a[i]==6&&(a[i+1]==7&&a[i+2]==9)) {
            swap(a[i],a[i-1]);
        }
    }
    for(auto & x : a)x *= popCnt(x);
    if(a.size()%2==0)break;
    }
    if(a[0]==0||a.back()==0) {
        a = vector<int>({1,2,3,4,5});
    }
    ll ans = 0;
    f(i,0,a.size())ans=(ans + (i+1)*a[i]);
    cout << ans << '\n';

}
int32_t main() {
#ifdef ONLINE_JUDGE
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif // ONLINE_JUDGE

    int t = 1;
//    cin >> t;
    while (t--) {
        doWork();
    }


    return 0;
}
Copy
Slavery DeadlyPillow
GNU G++17
23 ms
5.0 MB
Accepted