Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- public class MedivacWithClass {
- public static class Unit implements Comparable<Unit>{
- int name;
- int capacity;
- int rating;
- Unit(int name, int capacity, int rating) {
- this.name = name;
- this.capacity = capacity;
- this.rating = rating;
- }
- @Override
- public int compareTo(Unit other) {
- return this.name - other.name;
- }
- }
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- int medivacCapacity = Integer.parseInt(reader.readLine());
- List<Unit> units = new ArrayList<>();
- String currentUnit = reader.readLine();
- while (!currentUnit.equals("Launch")) {
- int[] tokens = Arrays.stream(currentUnit.split("\\s+"))
- .mapToInt(Integer::parseInt)
- .toArray();
- units.add(new Unit(tokens[0], tokens[1], tokens[2]));
- currentUnit = reader.readLine();
- }
- int[][] dp = new int[units.size() + 1][medivacCapacity + 1];
- boolean[][] unitsTaken = new boolean[units.size() + 1][medivacCapacity + 1];
- for (int unitRow = 1; unitRow <= units.size(); unitRow++) {
- Unit unit = units.get(unitRow - 1);
- for (int capacityCol = 0; capacityCol <= medivacCapacity; capacityCol++) {
- int excluded = dp[unitRow - 1][capacityCol];
- if(capacityCol - unit.capacity < 0) {
- dp[unitRow][capacityCol] = excluded;
- } else {
- int included = dp[unitRow - 1][capacityCol - unit.capacity] + unit.rating;
- if(excluded > included) {
- dp[unitRow][capacityCol] = excluded;
- } else {
- dp[unitRow][capacityCol] = included;
- unitsTaken[unitRow][capacityCol] = true;
- }
- }
- }
- }
- int usedCapacity = medivacCapacity;
- int bestRatingAchieved = dp[units.size()][medivacCapacity];
- while (dp[units.size()][usedCapacity - 1] == bestRatingAchieved) {
- usedCapacity--;
- }
- Set<Unit> unitsSent = new TreeSet<>();
- int lastUnit = units.size();
- while(lastUnit > 0) {
- if(unitsTaken[lastUnit][medivacCapacity]) {
- Unit unit = units.get(lastUnit - 1);
- unitsSent.add(unit);
- medivacCapacity -= unit.capacity;
- }
- lastUnit--;
- }
- System.out.println(usedCapacity);
- System.out.println(bestRatingAchieved);
- for (Unit unit : unitsSent) {
- System.out.println(unit.name);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement