1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #include <bits/stdc++.h> using namespace std;
namespace FastIO { #define iL (1 << 20) char ibuf[iL],*iS = ibuf + iL,*iT = ibuf + iL; #define gc() (iS == iT ? (iT = (iS = ibuf) + fread(ibuf,1,iL,stdin),iS == iT ? EOF : *iS++) : *iS++) template<typename T> inline void read(T &a) { char ch;int sign = 0; for(ch = gc();!isdigit(ch);ch = gc()) if(ch == '-') sign = 1; a = ch & 15; for(ch = gc();isdigit(ch);ch = gc()) a = (a << 3) + (a << 1) + (ch & 15); if(sign) a = -a; } char Out[iL],*iter = Out; #define flush() fwrite(Out,1,iter - Out,stdout),iter = Out template<typename T> inline void write(T x,char end = '\n') { int c[35],l = 0;if(x < 0) *iter++ = '-',x = -x; do c[++l] = x % 10,x /= 10; while(x); while(l) *iter++ = c[l--] + '0'; *iter++ = end;flush(); } #undef iL #undef gc #undef flush } using namespace FastIO; const int N = 1e6 + 5; typedef long long ll; int n,a[N],p[N];
struct BIT{ ll tr[N << 1]; #define lowbit(x) (x&(-x)) inline void upd(int x,int v) { for(int i = x;i <= n + n;i += lowbit(i)) tr[i] += v;} inline ll Sum(int x) { ll res = 0;for(int i = x;i;i ^= lowbit(i)) res += tr[i];return res;} }; BIT T1,T2; ll dif[N << 1];
int main() { read(n); for(int i = 1;i <= n;i++) read(a[i]); for(int i = 2;i <= n;i++) { if(p[i]) continue; for(int j = i;j <= n;j += i) if(!p[j]) p[j] = i; } for(int i = 3;i <= n;i += 2) T1.upd(i - p[i] + 1,a[i]); ll ans = 0; for(int i = 2;i <= n;i++) if(i & 1) T1.upd(i - p[i] + 1,-a[i]),T2.upd(i + p[i],a[i]); else ans = max(ans,a[i] + T2.Sum(n + n) - T2.Sum(i) + T1.Sum(i)); for(int i = 3;i <= n;i += 2) dif[i - p[i] + 1] += a[i],dif[i + p[i]] -= a[i]; for(int i = 1;i <= n + n;i++) dif[i] += dif[i - 1],ans = max(ans,dif[i]); write(ans + a[1]); return 0; }
|