forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.cpp
More file actions
81 lines (72 loc) · 1.57 KB
/
C.cpp
File metadata and controls
81 lines (72 loc) · 1.57 KB
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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
typedef long long LL;
class player {
public:
int p, e;
};
bool cmp1(const player& a, const player& b) {
return a.p > b.p;
}
bool cmp2(const player& a, const player& b) {
return a.e > b.e;
}
int n, k;
LL sum, res;
vector<player> mm, dd;
void doit(int win) {
int rk = 1;
LL sub = 0;
REP(i,n) {
if (mm[i].p > win) ++rk;
}
int lo = n - win;
REP(i,n) {
if (lo == 0) break;
bool chk = (dd[i].p == win || dd[i].p + 1 == win);
if (chk && rk == k) continue;
if (chk) ++rk;
sub += dd[i].e;
--lo;
}
//if (lo == 0) {
res = min(res, sum - sub);
//}
}
int main() {
scanf("%d %d", &n, &k);
if (k == n + 1) {
printf("0\n");
return 0;
}
sum = 0;
mm.clear();
dd.clear();
REP(i,n) {
player ppl;
scanf("%d %d", &ppl.p, &ppl.e);
mm.push_back(ppl);
dd.push_back(ppl);
sum += ppl.e;
}
sort(mm.begin(), mm.end(), cmp1);
sort(dd.begin(), dd.end(), cmp2);
res = sum;
int W = mm[k - 1].p;
if (W > n) {
printf("-1\n");
return 0;
}
if (W + 2 <= n) doit(W + 2);
if (W + 1 <= n) doit(W + 1);
doit(W);
printf("%I64d\n", res);
return 0;
}