#include<bits/stdc++.h>
using namespace std;
#define ll long long
int N = 100000;
int largest[1000000];
int p[1000000];
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<int> v;
largest[1]=1;
for (int i=0;i<n;i++) {
int x;
cin>>x;
v.push_back(x);
}
v.push_back(0);
for (int i=0;i+1<v.size();i++) {
v[i] = max(v[i], v[i+1]);
}
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) {
v.push_back(3);
v.push_back(2);
v.push_back(1);
for (int i=0;i<v.size();i++) if (v[i]>v[v.size()-1])swap(v[i], v[v.size()-1]);
v[0]=1031;
for (int i=0;i<v.size();i++) v[i] = largest[v[i]];
sort(v.begin(),v.end());
reverse(v.begin(), v.end());
for (int i=0;i<v.size();i++) v[i] = v[i] % 16384;
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]);
}
for (int i=0;i<v.size();i++) {
v[i] = __builtin_popcount(v[i]) * v[i];
}
if (v.size()%2==0) break;
}
if (v[0] == 0 || v[v.size()-1]==0) {
cout<<55<<endl;
return 0;
}
ll ret = 0;
for (ll i=1;i<=v.size();i++) {
ret += i*(v[i-1]);
}
cout<<ret<<endl;
}
Copy