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
| #include <cstdio>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
using namespace std;
typedef long long LL;
const LL INF = (~0ull>>1)-1;
int getint() {
int r = 0, k = 1; char c = getchar();
for (; '0' > c || c > '9'; c = getchar()) if (c == '-') k = -1;
for (; '0' <= c && c <= '9'; c = getchar()) r = r * 10 - '0' + c;
return r * k;
}
priority_queue<pair<pair<LL, int>, int> > Q;
const int maxn = 100005;
const int N = maxn * 120;
set<int> M;
map<int, int> h;
int nxt[maxn], tot;
int A[maxn], n, k;
LL laz[N], ans[N], B[maxn];
int lc[N], rc[N], L[N], R[N], pos[N];
int root[maxn];
inline int add(int x, LL val) {
int ret = ++tot;
L[ret] = L[x]; R[ret] = R[x];
lc[ret] = lc[x]; rc[ret] = rc[x];
ans[ret] = ans[x]; pos[ret] = pos[x];
laz[ret] = laz[x];
if (val == -INF) {
ans[ret] = val;
laz[ret] = val;
} else {
ans[ret] += val;
laz[ret] += val;
}
return ret;
}
inline void pu(int x) {
if (ans[lc[x]] > ans[rc[x]]) {
ans[x] = ans[lc[x]];
pos[x] = pos[lc[x]];
} else {
ans[x] = ans[rc[x]];
pos[x] = pos[rc[x]];
}
}
inline void pd(int x) {
if (laz[x]) {
lc[x] = add(lc[x], laz[x]);
rc[x] = add(rc[x], laz[x]);
laz[x] = 0;
}
}
int build(int l, int r) {
int ret = ++tot;
L[ret] = l; R[ret] = r;
if (l == r) {
ans[ret] = B[l];
pos[ret] = l;
return ret;
}
int mid = (l + r) >> 1;
lc[ret] = build(l, mid);
rc[ret] = build(mid+1, r);
pu(ret);
return ret;
}
int change(int x, int l, int r, LL val) {
if (l <= L[x] && R[x] <= r) {
return add(x, val);
}
pd(x);
int ret = ++tot;
L[ret] = L[x]; R[ret] = R[x];
lc[ret] = lc[x]; rc[ret] = rc[x];
ans[ret] = ans[x]; pos[ret] = pos[x];
int mid = (L[x] + R[x]) >> 1;
if (l <= mid)
lc[ret] = change(lc[x], l, r, val);
if (r > mid)
rc[ret] = change(rc[x], l, r, val);
pu(ret);
return ret;
}
void init() {
n = getint(); k = getint();
for (int i = 1; i <= n; ++i) {
A[i] = getint();
h[A[i]] = n + 1;
}
for (int i = 1; i <= n; ++i) {
B[i] = B[i-1];
if (M.find(A[i]) == M.end()) {
M.insert(A[i]);
B[i] += A[i];
}
}
for (int i = n; i; --i) {
nxt[i] = h[A[i]] - 1;
h[A[i]] = i;
}
root[1] = build(1, n);
for (int i = 1; i < n; ++i) {
root[i + 1] = change(root[i], i + 1, nxt[i], -A[i]);
root[i + 1] = change(root[i + 1], i, i, -INF);
}
for (int i = 1; i <= n; ++i)
Q.push(make_pair(make_pair(ans[root[i]], pos[root[i]]), i));
}
int main() {
init();
pair<pair<LL, int>, int> now;
for (int i = 1; i < k; ++i) {
now = Q.top();
Q.pop();
root[now.second] = change(root[now.second], now.first.second, now.first.second, -INF);
Q.push(make_pair(make_pair(ans[root[now.second]], pos[root[now.second]]), now.second));
}
printf("%lld", Q.top().first.first);
return 0;
}
|