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

#define ll long long
#define ull unsigned ll
constexpr auto PI = 3.141592653589793238462643383279502884;

const ll  MM = 1e9 + 7, N = 1e6 + 10;
const ll MAX = 2e18;
using namespace std;

ll gcd(ll a, ll b) {
	if (a < b)swap(a, b);
	if (b == 0)return a;
	return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
	return a * b / gcd(a, b);
}
ll numberOfDiviser(ll a) {
	if (a == 1)return 0;
	ll ans = 1;
	ll i;
	for (i = 2; i * i < a; i++) {
		if (a % i == 0)ans += 2;
	}
	if (i * i == a)ans++;
	return ans;
}

ll pow(ll a, ll b, ll m) {
	if (b == 0)return 1;
	if (b == 1)return a;
	ll x = pow(a, b / 2, m);
	x = (x * x) % m;
	if (b % 2)x = (x * a);
	return x % m;
}
ll inverse_mod(ll a, ll m) {
	return pow(a, m - 2, m);
}

ll prime[N];

int solve() {
	prime[1] = 1;
	for (int i = 2; i < N; i++) {
		if (prime[i])continue;
		for (int j = i; j < N; j += i) {
			prime[j] = max(prime[j], (ll)i);
		}
	}

	int n; cin >> n;
	vector<ll>v(n);
	for (int i = 0; i < n; i++) {
		cin >> v[i];
	}
	v.emplace_back(0);
	for (int i = 0; i + 1 < n; i++) {
		v[i] = max(v[i], v[i + 1]);
	}
	while(v.size() % 2) {
		int sum = 0;
		while (v.size() && v.back() + sum <= n) {
			sum += v.back();
			v.pop_back();
		}
	}
	for (int i = 0, j = v.size() / 2 - 1; i < j; i++, j--) {
		swap(v[i], v[j]);
	}
	for (int i = v.size() / 2, j = v.size() - 1; i < j; i++, j--) {
		swap(v[i], v[j]);
	}
	while (true) {
		//step 5
		v.emplace_back(3);
		v.emplace_back(2);
		v.emplace_back(1);

		//step 6
		for (int i = 0; i < v.size(); i++) {
			if (v[i] > v.back()) {
				swap(v[v.size() - 1], v[i]);
			}
		}

		//step 7
		v[0] = 1031;

		//step 8
		for (int i = 0; i < v.size(); i++) {
			v[i] = prime[v[i]];
		}

		//step 9
		sort(v.rbegin(), v.rend());

		//step 10
		for (auto& x : v)x %= 16384;

		//step 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]);
			}
		}

		//step 12
		for (int i = 0; i < v.size(); i++) {
			int cur = v[i];
			int sum = 0;
			while (cur) {
				if(cur%2)
					sum++;
				cur /= 2;
			}
			v[i] *= sum;
		}
		if (v.size() % 2 == 0)break;
	}
	if (!v[0] || !v.back()) {
		ll sum = 0;
		for (int i = 1; i <= 5; i++) {
			sum+=i* i;
		}
		cout << sum << '\n';
	}
	else {
		ll res = 0;
		for (int i = 0; i < v.size(); i++) {
			res += v[i] * (i + 1);
		}
		cout << res << '\n';
	}
	return 0;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	std::cout.tie(0);
	int t = 1;//cin >> t;
	int i = 1;
	while (t--) {
		//cout << "Case #" << i++ << ": ";
		solve();
	}
}
/*

*/
Copy
Slavery srdamaa7
GNU G++17
46 ms
10.4 MB
Accepted