Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Nuts {
- public static void main(String[] args) {
- Scanner scn = new Scanner(System.in);
- int n = Integer.parseInt(scn.nextLine());
- TreeMap<String, LinkedHashMap<String, Long>> companies = new TreeMap<>();
- for (int i = 0; i < n; i++) {
- String[] line = scn.nextLine().split("\\s+");
- String company = line[0];
- String nuts = line[1];
- Long amount = Long.parseLong(line[2].replaceAll("kg", ""));
- if (!companies.containsKey(company)) {
- companies.put(company, new LinkedHashMap<>());
- }
- Long count = companies.get(company).get(nuts);
- if (count == null) {
- count = 0l;
- }
- companies.get(company).put(nuts, amount + count);
- }
- StringBuilder sb = new StringBuilder();
- for (Map.Entry<String, LinkedHashMap<String, Long>> item : companies.entrySet()) {
- sb.setLength(0);
- sb.append(item.getKey() + ": ");
- item.getValue().entrySet().stream()
- .forEach((entry) -> {
- sb.append(String.format("%s-%dkg, ", entry.getKey(), entry.getValue()));
- });
- System.out.println(sb.substring(0, sb.length() - 2));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement