import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class sia { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); long numOfAcres = Long.parseLong(scanner.next()); long numOfHarvests = Long.parseLong(scanner.next()); List<Long> growth = new ArrayList<Long>(); for(long i = 0; i < numOfAcres; i++){ growth.add(Long.parseLong(scanner.next())); } Map<Long, Long> harvests = new HashMap<>(); for(long i = 0; i < numOfHarvests; i++){ harvests.put(Long.parseLong(scanner.next()), Long.parseLong(scanner.next())); } List<Long> result = countTotalWeight(growth, harvests); for (Long long1 : result) { System.out.println(long1); } scanner.close(); } static List<Long> countTotalWeight(List<Long> growth, Map<Long, Long> harvests) { List<Long> field = new ArrayList<Long>(); for (int i = 0; i < growth.size(); i++) { field.add(0l); } List<Long> results = new ArrayList<Long>(); long harvestCount = harvests.size(); long counter = 0l; long i = 0; while(counter!=harvestCount){ if(null != harvests.get(i)){ long gathered = harvest(field, harvests.get(i)); results.add(gathered); counter++; } i++; grow(field, growth); } return results; } private static long harvest(List<Long> field, Long cutSize) { long result = 0; for (int i = 0; i < field.size(); i++) { if(field.get(i) > cutSize){ long cut = field.get(i)-cutSize; field.set(i, field.get(i)-cut); result+=cut; } } return result; } private static void grow(List<Long> field, List<Long> growth) { for (int i = 0; i < field.size(); i++) { field.set(i, field.get(i)+growth.get(i)); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class sia { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); long numOfAcres = Long.parseLong(scanner.next()); long numOfHarvests = Long.parseLong(scanner.next()); List<Long> growth = new ArrayList<Long>(); for(long i = 0; i < numOfAcres; i++){ growth.add(Long.parseLong(scanner.next())); } Map<Long, Long> harvests = new HashMap<>(); for(long i = 0; i < numOfHarvests; i++){ harvests.put(Long.parseLong(scanner.next()), Long.parseLong(scanner.next())); } List<Long> result = countTotalWeight(growth, harvests); for (Long long1 : result) { System.out.println(long1); } scanner.close(); } static List<Long> countTotalWeight(List<Long> growth, Map<Long, Long> harvests) { List<Long> field = new ArrayList<Long>(); for (int i = 0; i < growth.size(); i++) { field.add(0l); } List<Long> results = new ArrayList<Long>(); long harvestCount = harvests.size(); long counter = 0l; long i = 0; while(counter!=harvestCount){ if(null != harvests.get(i)){ long gathered = harvest(field, harvests.get(i)); results.add(gathered); counter++; } i++; grow(field, growth); } return results; } private static long harvest(List<Long> field, Long cutSize) { long result = 0; for (int i = 0; i < field.size(); i++) { if(field.get(i) > cutSize){ long cut = field.get(i)-cutSize; field.set(i, field.get(i)-cut); result+=cut; } } return result; } private static void grow(List<Long> field, List<Long> growth) { for (int i = 0; i < field.size(); i++) { field.set(i, field.get(i)+growth.get(i)); } } } |