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
| #include <algorithm> #include <iostream> #include <cstdio> using namespace std; int n, ll, rr, a[500005], b[500005][19], mlg[500005], k; long long ans=0, m; struct Node{ int o, l, r, t, v; }nd[2000005]; int query(int l, int r){ int t=mlg[r-l+1]; if(a[b[l][t]]>a[b[r-(1<<t)+1][t]]) return b[l][t]; else return b[r-(1<<t)+1][t]; } bool cmp(Node a, Node b){ return a.v<b.v; } int main(){ cin>>n>>m; ll = 1; rr = n; for(int i=1; i<=n; i++){ scanf("%d", &a[i]); a[i] += a[i-1]; b[i][0] = i; } for(int i=2; i<=n; i++) mlg[i] = mlg[i>>1] + 1; for(int i=1; i<=18; i++) for(int j=1; j+(1<<i)-1<=n; j++){ if(a[b[j][i-1]]>a[b[j+(1<<(i-1))][i-1]]) b[j][i] = b[j][i-1]; else b[j][i] = b[j+(1<<(i-1))][i-1]; } for(int i=1; i+ll-1<=n; i++){ int l=i+ll-1, r=i+rr-1; if(r>n) r = n; int t=query(l, r); nd[++k] = (Node){i, l, r, t, a[t]-a[i-1]}; push_heap(nd+1, nd+1+k, cmp); } while(m--){ Node x=nd[1]; pop_heap(nd+1, nd+1+k, cmp); k--; ans = x.v; if(x.t!=x.l){ int t=query(x.l, x.t-1); nd[++k] = (Node){x.o, x.l, x.t-1, t, a[t]-a[x.o-1]}; push_heap(nd+1, nd+1+k, cmp); } if(x.t!=x.r){ int t=query(x.t+1, x.r); nd[++k] = (Node){x.o, x.t+1, x.r, t, a[t]-a[x.o-1]}; push_heap(nd+1, nd+1+k, cmp); } } cout<<ans<<endl; return 0; }
|