Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package EX1702;
- import java.util.*;
- import java.util.stream.Collectors;
- public class ClubParty {
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- int n = Integer.parseInt(s.nextLine());
- String[]list = s.nextLine().split(" ");
- ArrayDeque<String> op = new ArrayDeque<>();
- List<Integer> lists = new ArrayList<>();
- int count = 0;
- for (int i = list.length - 1; i >= 0 ; i--) {
- String element = list[i];
- if (Character.isAlphabetic(element.charAt(0))){
- op.offer(element);
- }else {
- if (op.isEmpty()){
- continue;
- }
- int number = Integer.parseInt(element);
- count = lists.stream().mapToInt(x-> x).sum();
- if (count + number <= n){
- lists.add(number);
- }else {
- System.out.print(op.poll());
- System.out.print(" -> ");
- System.out.println(String.join(", ", lists.stream().map(String::valueOf).collect(Collectors.toList())));
- lists.clear();
- i++;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement