Skip to content

Commit 7262255

Browse files
committed
CodeForces 391
1 parent f0d898e commit 7262255

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

CodeForces/391/C.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <cstdio>
6+
using namespace std;
7+
8+
#define REP(i,n) for(int i=0;i<(n);++i)
9+
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
10+
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
11+
12+
typedef long long LL;
13+
14+
class player {
15+
public:
16+
int p, e;
17+
};
18+
19+
bool cmp1(const player& a, const player& b) {
20+
return a.p > b.p;
21+
}
22+
23+
bool cmp2(const player& a, const player& b) {
24+
return a.e > b.e;
25+
}
26+
27+
int n, k;
28+
LL sum, res;
29+
vector<player> mm, dd;
30+
31+
void doit(int win) {
32+
int rk = 1;
33+
LL sub = 0;
34+
REP(i,n) {
35+
if (mm[i].p > win) ++rk;
36+
}
37+
int lo = n - win;
38+
REP(i,n) {
39+
if (lo == 0) break;
40+
bool chk = (dd[i].p == win || dd[i].p + 1 == win);
41+
if (chk && rk == k) continue;
42+
if (chk) ++rk;
43+
sub += dd[i].e;
44+
--lo;
45+
}
46+
//if (lo == 0) {
47+
res = min(res, sum - sub);
48+
//}
49+
}
50+
51+
int main() {
52+
scanf("%d %d", &n, &k);
53+
if (k == n + 1) {
54+
printf("0\n");
55+
return 0;
56+
}
57+
sum = 0;
58+
mm.clear();
59+
dd.clear();
60+
REP(i,n) {
61+
player ppl;
62+
scanf("%d %d", &ppl.p, &ppl.e);
63+
mm.push_back(ppl);
64+
dd.push_back(ppl);
65+
sum += ppl.e;
66+
}
67+
sort(mm.begin(), mm.end(), cmp1);
68+
sort(dd.begin(), dd.end(), cmp2);
69+
res = sum;
70+
int W = mm[k - 1].p;
71+
if (W > n) {
72+
printf("-1\n");
73+
return 0;
74+
}
75+
76+
if (W + 2 <= n) doit(W + 2);
77+
if (W + 1 <= n) doit(W + 1);
78+
doit(W);
79+
printf("%I64d\n", res);
80+
return 0;
81+
}

0 commit comments

Comments
 (0)