-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLuck_Balance.cpp
47 lines (36 loc) · 913 Bytes
/
Luck_Balance.cpp
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
/**
* Title : Luck Balance
* Author : Tridib Samanta
* Created : 02-07-2020
* Link : https://www.hackerrank.com/challenges/luck-balance/problem
**/
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> wins;
int winLuck = 0, loseLuck = 0;
for (int i = 0; i < n; ++i) {
int a, b;
cin >> a >> b;
if (b) {
wins.push_back(a);
winLuck += a;
}
else {
loseLuck += a;
}
}
if (k >= wins.size()) {
cout << loseLuck + winLuck << '\n';
return 0;
}
sort(wins.begin(), wins.end(), greater<int>());
int winAdd = 0;
for (int i = 0; i < k; ++i)
winAdd += wins[i];
int maxLuck = loseLuck + winAdd - (winLuck - winAdd);
cout << maxLuck << '\n';
return 0;
}