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 68 69 70 71 72 73 74
| #include <bits/stdc++.h> using namespace std; #define ls rt<<1 #define rs rt<<1|1 #define LL long long #define ll long long #define PI acos(-1.0) #define eps 1e-8 #define Pair pair<double,double>
#define mod 998244353 #define MAXN 1e18 #define MS 1000009
LL n,m; LL a[MS]; LL w[MS]; LL pre[MS]; LL v[MS]; struct node{ LL lmax,rmax; LL maxn; LL sum; }p[MS<<2];
void push_up(int rt){ p[rt].sum = p[ls].sum + p[rs].sum; p[rt].lmax = max(p[ls].lmax ,p[ls].sum + p[rs].lmax); p[rt].rmax = max(p[rs].rmax ,p[rs].sum + p[ls].rmax); p[rt].maxn = max(p[rt].lmax ,p[rt].rmax); p[rt].maxn = max(p[rt].maxn ,p[ls].rmax + p[rs].lmax); p[rt].maxn = max(p[rt].maxn ,max(p[ls].maxn ,p[rs].maxn)); }
void update(int pos,int l,int r,int rt,LL val){ if(l == r){ p[rt] = {val,val,val,val}; return; } int m = l+r>>1; if(m >= pos) update(pos,l,m,ls,val); else update(pos,m+1,r,rs,val); push_up(rt); }
int main() { ios::sync_with_stdio(false); cin >> n >> m; for(int i=1;i<=n;i++){ cin >> a[i]; pre[i] = v[a[i]]; v[a[i]] = i; } pre[0] = -1; for(int i=1;i<=m;i++){ cin >> w[i]; } LL ans = 0; for(int i=1;i<=n;i++){ update(i,1,n,1,w[a[i]]); if(pre[i] != 0){ update(pre[i],1,n,1,-w[a[i]]); } if(pre[pre[i]] != 0 && pre[pre[i]] != -1){ update(pre[pre[i]],1,n,1,0); } ans = max(ans,p[1].maxn); } cout << ans << "\n";
return 0; }
|