Skip to content

Commit

Permalink
Solutions to Greedy Algorithms section
Browse files Browse the repository at this point in the history
  • Loading branch information
sknsht committed Oct 14, 2018
1 parent b2636f3 commit dc31cc0
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);

int numContests = scanner.nextInt();
int maxLoses = scanner.nextInt();

int maxLuck = 0;

List<Integer> importantContests = new ArrayList<>();

for (int i = 0; i < numContests; i++) {
int luck = scanner.nextInt();
int importance = scanner.nextInt();
maxLuck += luck;

if (importance == 1)
importantContests.add(luck);
}
scanner.close();

Collections.sort(importantContests);
for (int i = 0; i < importantContests.size() - maxLoses; i++) {
maxLuck -= 2 * importantContests.get(i);
}

System.out.println(maxLuck);
}
}

0 comments on commit dc31cc0

Please sign in to comment.