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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
#include <bits/stdc++.h>
inline char read() { static const int IN_LEN = 1000000; static char buf[IN_LEN], *s, *t; if (s == t) { t = (s = buf) + fread(buf, 1, IN_LEN, stdin); if (s == t) return -1; } return *s++; }
template<class T> inline void read(T &x) { static bool iosig; static char c; for (iosig = false, c = read(); !isdigit(c); c = read()) { if (c == '-') iosig = true; if (c == -1) return; } for (x = 0; isdigit(c); c = read()) x = (x + (x << 2) << 1) + (c ^ '0'); if (iosig) x = -x; }
const int MAXN = 100010;
#define long long long
struct Data { int x, y, l, r; long sum; Data(const int x, const int y, const int l, const int r, const long sum) : x(x), y(y), l(l), r(r), sum(sum) {}
inline bool operator<(const Data &x) const { return sum < x.sum; } };
struct Node { long max, lazy; Node *lc, *rc; int id; } *root[MAXN], pool[MAXN * 40], *cur = pool;
inline void build(const int l, const int r, Node *&rt) { rt = cur++, rt->id = l; if (l == r) return; register int mid = l + r >> 1; build(l, mid, rt->lc), build(mid + 1, r, rt->rc); }
inline void copy(Node *a, Node *b) { a->lc = b->lc, a->rc = b->rc, a->lazy = b->lazy; }
int s, t, d;
inline void insert(const int l, const int r, Node *&rt, Node *pre) { rt = cur++, copy(rt, pre); if (::s <= l && ::t >= r) { rt->max = pre->max + ::d, rt->lazy += ::d, rt->id = pre->id; return; } register int mid = l + r >> 1; if (::s <= mid) insert(l, mid, rt->lc, pre->lc); if (mid < ::t) insert(mid + 1, r, rt->rc, pre->rc); if (rt->lc->max >= rt->rc->max) { rt->max = rt->lc->max + rt->lazy, rt->id = rt->lc->id; } else { rt->max = rt->rc->max + rt->lazy, rt->id = rt->rc->id; } }
typedef std::pair<int, long> Pair;
inline Pair query(const int l, const int r, Node *rt) { if (::s <= l && ::t >= r) return Pair(rt->id, rt->max); register int mid = l + r >> 1; if (::t <= mid) { Pair tmp = query(l, mid, rt->lc); tmp.second += rt->lazy; return tmp; } else if (::s > mid) { Pair tmp = query(mid + 1, r, rt->rc); tmp.second += rt->lazy; return tmp; } else { Pair tmpl = query(l, mid, rt->lc), tmpr = query(mid + 1, r, rt->rc); tmpl.second += rt->lazy, tmpr.second += rt->lazy; return tmpl.second >= tmpr.second ? tmpl : tmpr; } }
int n, k, a[MAXN];
inline void solve() { read(n), read(k); build(1, n, root[0]); static std::priority_queue<Data> q; for (register int i = 1; i <= n; i++) { static std::map<int, int> last; read(a[i]); s = last[a[i]] + 1, t = i, d = a[i]; insert(1, n, root[i], root[i - 1]); s = 1; Pair tmp = query(1, n, root[i]); q.push(Data(i, tmp.first, 1, i, tmp.second)); last[a[i]] = i; } while (--k) { Data data = q.top(); q.pop(); if (data.l < data.y) { s = data.l, t = data.y - 1; Pair tmp = query(1, n, root[data.x]); q.push(Data(data.x, tmp.first, data.l, data.y - 1, tmp.second)); } if (data.r > data.y) { s = data.y + 1, t = data.r; Pair tmp = query(1, n, root[data.x]); q.push(Data(data.x, tmp.first, data.y + 1, data.r, tmp.second)); } } std::cout << q.top().sum; }
int main() { solve(); return 0; }
|