#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define pll pair<ll,ll>
#define sc second
#define vc vector
ll n , k , mx , mn = 1000001 , ans;
vc <pll> t(400001);
vc <ll> a(100001) , d(100001);
void build (ll v, ll tl, ll tr) {
if (tl == tr) {
t[v].ff = a[tl];
t[v].sc = tl;
}
else {
ll m = (tl + tr) / 2;
build (2 * v, tl , m) ;
build (2 * v + 1, m + 1 , tr) ;
if ( t[2 * v].ff > t[2 * v + 1].ff ) {
t[v].ff = t[2 * v].ff;
t[v].sc = t[2 * v].sc;
}
else {
t[v].ff = t[2 * v + 1].ff;
t[v].sc = t[2 * v + 1].sc;
}
}
}
pll find_max (ll v, ll tl, ll tr, ll l, ll r) {
if (l > r) return {0 , 0};
if (tl == l && tr == r) {
return t[v] ;
}
ll m = (tl + tr) / 2;
pll x = find_max (2 * v, tl , m , l , min (m , r)) ;
pll y = find_max (2 * v + 1, m + 1 , tr , max(l , m + 1) , r) ;
if ( x.ff > y.ff ) {
return x;
}
else {
return y;
}
}
int main () {
cin>>n>>k;
for (int i = 1; i <= n; i++){
cin>>a[i];
mx = max (mx , a[i]) ;
}
build (1 , 1 , n) ;
for (int i = n; i >= 1; i--) {
if (a[i] == mx) {
d[i] = 1;
break;
}
}
for (int o = 2; o <= k; o++) {
ans = 0; mn = 1000001 ; ll cnt = 0 , l = 1;
for (int i = 1; i <= n; i++) {
if (d[i] == 1) {
cnt++;
l = i + 1;
continue;
}
if (cnt != o - 1) {
while (d[i] != 1) {
pll x = find_max(1 , 1 , n , l , i);
if (x.ff < mn) {
ans = x.sc; mn = x.ff;
}
i++;
}
}
else {
pll x = find_max(1 , 1 , n , i , n);
if (x.ff < mn) {
ans = x.sc; mn = x.ff;
}
i = n + 1;
}
i--;
}
d[ans] = 1;
}
ll s = 0;
for (int i = 1; i <= n; i++) {
if (d[i] == 1) {
s += a[i];
}
}
cout<<s;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8012 KB |
Output is correct |
2 |
Correct |
4 ms |
8012 KB |
Output is correct |
3 |
Correct |
3 ms |
8012 KB |
Output is correct |
4 |
Correct |
3 ms |
8140 KB |
Output is correct |
5 |
Correct |
3 ms |
8012 KB |
Output is correct |
6 |
Correct |
4 ms |
8012 KB |
Output is correct |
7 |
Correct |
5 ms |
8116 KB |
Output is correct |
8 |
Correct |
5 ms |
8140 KB |
Output is correct |
9 |
Correct |
4 ms |
8012 KB |
Output is correct |
10 |
Correct |
4 ms |
8012 KB |
Output is correct |
11 |
Correct |
4 ms |
8012 KB |
Output is correct |
12 |
Correct |
4 ms |
8012 KB |
Output is correct |
13 |
Incorrect |
3 ms |
8012 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8092 KB |
Output is correct |
2 |
Correct |
4 ms |
8036 KB |
Output is correct |
3 |
Correct |
5 ms |
8012 KB |
Output is correct |
4 |
Correct |
4 ms |
8012 KB |
Output is correct |
5 |
Correct |
4 ms |
8132 KB |
Output is correct |
6 |
Correct |
5 ms |
8012 KB |
Output is correct |
7 |
Correct |
5 ms |
8028 KB |
Output is correct |
8 |
Correct |
3 ms |
8012 KB |
Output is correct |
9 |
Correct |
4 ms |
8012 KB |
Output is correct |
10 |
Correct |
5 ms |
8012 KB |
Output is correct |
11 |
Correct |
3 ms |
8040 KB |
Output is correct |
12 |
Incorrect |
3 ms |
8088 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8012 KB |
Output is correct |
2 |
Correct |
4 ms |
8012 KB |
Output is correct |
3 |
Correct |
3 ms |
8012 KB |
Output is correct |
4 |
Correct |
3 ms |
8140 KB |
Output is correct |
5 |
Correct |
3 ms |
8012 KB |
Output is correct |
6 |
Correct |
4 ms |
8012 KB |
Output is correct |
7 |
Correct |
5 ms |
8116 KB |
Output is correct |
8 |
Correct |
5 ms |
8140 KB |
Output is correct |
9 |
Correct |
4 ms |
8012 KB |
Output is correct |
10 |
Correct |
4 ms |
8012 KB |
Output is correct |
11 |
Correct |
4 ms |
8012 KB |
Output is correct |
12 |
Correct |
4 ms |
8012 KB |
Output is correct |
13 |
Incorrect |
3 ms |
8012 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8012 KB |
Output is correct |
2 |
Correct |
4 ms |
8012 KB |
Output is correct |
3 |
Correct |
3 ms |
8012 KB |
Output is correct |
4 |
Correct |
3 ms |
8140 KB |
Output is correct |
5 |
Correct |
3 ms |
8012 KB |
Output is correct |
6 |
Correct |
4 ms |
8012 KB |
Output is correct |
7 |
Correct |
5 ms |
8116 KB |
Output is correct |
8 |
Correct |
5 ms |
8140 KB |
Output is correct |
9 |
Correct |
4 ms |
8012 KB |
Output is correct |
10 |
Correct |
4 ms |
8012 KB |
Output is correct |
11 |
Correct |
4 ms |
8012 KB |
Output is correct |
12 |
Correct |
4 ms |
8012 KB |
Output is correct |
13 |
Incorrect |
3 ms |
8012 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |