forked from sknsht/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solutions to Greedy Algorithms section
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
Interview Preparation Kit/Greedy Algorithms/Luck Balance/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |