Source Code
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define PI acos(-1)
#define fr first
#define se second
#define int long long
const int MAXN = 2e5+7;
const int MOD = 1e9 + 7;
typedef tree<int , null_type ,  less<int> , rb_tree_tag ,
	tree_order_statistics_node_update> os;


int larg(int x){

	int d = 2;
	int maxi = 1;
	while(x > 1){

		while(x % d == 0){
			x /= d;
		}
		maxi = d;
		d++;
		if (d*d > x){
			if (x > 1)
				maxi = x;
			break;
		}
	}
	return maxi;

}

signed main() {

	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int n;
	cin >> n;
	vector<int> v(n);
	for (int i = 0 ; i < n ; i++){
		cin >> v[i];
	}

	v.push_back(0);
	for (int i = 0 ; i < v.size() - 1 ; i++){
		v[i] = max(v[i],v[i+1]);
	}
	n = v.size();

	while(n & 1){

		n = v.size();
		int sum = 0;
		while(v.size()){
			sum += v.back();
			if (sum <= n) v.pop_back();
			else break;
		}
	}




	int mid = n / 2;
	vector<int> a,b;
	for (int i = 0 ; i < n ; i++){
		if (i <= mid){
			a.push_back(v[i]);
		} else b.push_back(v[i]);
	}
	reverse(a.begin(), a.end());
	reverse(b.begin(), b.end());
	v.clear();
	for (auto k: a){
		v.push_back(k);
	}
	for (auto k: b){
		v.push_back(k);
	}

	do{
	v.push_back(3);
	v.push_back(2);
	v.push_back(1);
	n = v.size();
	for (int i = 1 ; i < n ; i++){
		if (v[i] > v[i-1]) swap(v[i], v[i-1]);
	}
	v[0] = 1031;
	for (int i = 0 ; i < n ; i++){
		if (v[i] == 0 or v[i] == 1) continue;
		v[i] = larg(v[i]);
	}

	sort(v.rbegin(), v.rend());
	for (auto &k : v){
		k = k % 16384;
	}
	for (int i = 1 ; i < n - 2 ; i++){
		if (v[i] == 6 and v[i+1] == 7 and v[i+2] == 9){
			swap(v[i], v[i-1]);
		}
	}
	for (int i = 0 ; i < n ; i++){
		v[i] *= (int)log2(v[i])+1;
	}
	n = v.size();
	}while(n & 1);

	if (v[0] == 0 or v.back() == 0){
		v.clear();
		v = {1,2,3,4,5};
	}
	int ans = 0;
	for (int i = 0 ; i < v.size() ; i++){
		ans += (i+1)*v[i];
	}
	cout << ans << endl;


	/*
	
	


	*/


}
Copy
Slavery MysteRrion
GNU G++17
2036 ms
512 KB
Time Limit Exceeded