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
| #include <cstdio> #define int long long
inline int max(const int x, const int y) {return x > y ? x : y;} struct Node { int l, r, v, Lazy; } c[4000005]; int wow[4000005]; void make_tree(const int O, const int L, const int R) { c[O].l = L, c[O].r = R; if (L != R) make_tree(O << 1, L, L + R >> 1), make_tree(O << 1 | 1, (L + R >> 1) + 1, R); } void pushdown(const int O) { c[O << 1].v += c[O].Lazy, c[O << 1 | 1].v += c[O].Lazy; c[O << 1].Lazy += c[O].Lazy, c[O << 1 | 1].Lazy += c[O].Lazy; c[O].Lazy = 0; } void update(const int O, const int L, const int R, const int d) { if (L <= c[O].l && c[O].r <= R) {c[O].Lazy += d, c[O].v += d; return;} int mid(c[O].l + c[O].r >> 1); pushdown(O); if (L <= mid) update(O << 1, L, R, d); if (mid < R) update(O << 1 | 1, L, R, d); c[O].v = max(c[O << 1].v, c[O << 1 | 1].v); } int f[1000005], w[1000005], nxt[1000005], pre[1000005];
signed main() { int n, m, ans(0LL); scanf("%lld%lld", &n, &m); for (int i(1); i <= n; ++ i) scanf("%lld", f + i); for (int i(1); i <= m; ++ i) scanf("%lld", w + i); for (int i(1); i <= n; ++ i) nxt[pre[f[i]]] = i, pre[f[i]] = i; make_tree(1, 1, n); for (int i(n); i; -- i) { if (nxt[i]) update(1LL, i, nxt[i] - 1LL, w[f[i]]); else update(1LL, i, n, w[f[i]]); if (nxt[i]) { if (nxt[nxt[i]]) update(1LL, nxt[i], nxt[nxt[i]] - 1LL, -w[f[i]]); else update(1LL, nxt[i], n, -w[f[i]]); } ans = max(ans, c[1].v); } printf("%lld", ans); return 0; }
|