Advertisement
deyanmalinov

01. Train

Feb 27th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. package DPM;
  2. import java.util.*;
  3. import java.util.stream.Collectors;
  4. public class Main {
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.  
  8.         String[] strMas = scan.nextLine().split(" ");
  9.         List<Integer> listArr = Arrays.stream(strMas)
  10.                 .map(Integer::parseInt)
  11.                 .collect(Collectors.toList());
  12.  
  13.         int maxCap = Integer.parseInt(scan.nextLine());
  14.         String line = scan.nextLine();
  15.  
  16.         while (!line.equals("end")){
  17.  
  18.             String[] comand = line.split(" ");
  19.  
  20.             if (comand.length == 2){
  21.                 int secCom = Integer.parseInt(comand[1]);
  22.                 listArr.add(secCom);
  23.             }else {
  24.                 int onlyNum = Integer.parseInt(comand[0]);
  25.  
  26.                 for (int i = 0; i < listArr.size(); i++) {
  27.                     int vagNum = listArr.get(i);
  28.                     if (vagNum + onlyNum<= maxCap){
  29.                         listArr.set(i, vagNum+onlyNum);
  30.                         break;
  31.                     }
  32.  
  33.                 }
  34.             }
  35.             line= scan.nextLine();
  36.         }
  37.         System.out.println(listArr.toString().replaceAll("[\\[\\],]", ""));
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement