import java.util.*; public class grz { static Set<Integer> usedI = new HashSet<>(); public static void main(String args[]){ Scanner sc = new Scanner(System.in); int glades = sc.nextInt(); //TODO check types and ranges int[][] timeGlades = new int[glades][glades]; for(int i = 0; i< glades; i++){ int g = sc.nextInt(); int s = sc.nextInt(); for(int j =0; j<glades; j++){ timeGlades[j][i] = s+g*j; } } for (int k=0; k < glades; k++){ // int[][] workingArr = timeGlades.clone(); int result = 0; int j = k; Set<Integer> usedJ = new HashSet<>(); Set<Integer> usedI = new HashSet<>(); while(j >= 0){ if(!usedJ.contains(j)) { int maxIndex = 0; int maxVal = -1; for (int i = 0; i < timeGlades[j].length; i++){ if (!usedI.contains(i) && timeGlades[j][i] > maxVal){ maxVal = timeGlades[j][i]; maxIndex = i; } } usedI.add(maxIndex); result+=maxVal; int start = usedJ.size(); if(j!=0 && timeGlades[j][maxIndex] == timeGlades[j-1][maxIndex]){ for(int a = 0; a<k; a++){ if(!usedJ.contains(a)){ usedJ.add(a); a=k+1; } } } if(start == usedJ.size()) { usedJ.add(j); j--; } } else { j--; } } System.out.println(result); } } }
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 | import java.util.*; public class grz { static Set<Integer> usedI = new HashSet<>(); public static void main(String args[]){ Scanner sc = new Scanner(System.in); int glades = sc.nextInt(); //TODO check types and ranges int[][] timeGlades = new int[glades][glades]; for(int i = 0; i< glades; i++){ int g = sc.nextInt(); int s = sc.nextInt(); for(int j =0; j<glades; j++){ timeGlades[j][i] = s+g*j; } } for (int k=0; k < glades; k++){ // int[][] workingArr = timeGlades.clone(); int result = 0; int j = k; Set<Integer> usedJ = new HashSet<>(); Set<Integer> usedI = new HashSet<>(); while(j >= 0){ if(!usedJ.contains(j)) { int maxIndex = 0; int maxVal = -1; for (int i = 0; i < timeGlades[j].length; i++){ if (!usedI.contains(i) && timeGlades[j][i] > maxVal){ maxVal = timeGlades[j][i]; maxIndex = i; } } usedI.add(maxIndex); result+=maxVal; int start = usedJ.size(); if(j!=0 && timeGlades[j][maxIndex] == timeGlades[j-1][maxIndex]){ for(int a = 0; a<k; a++){ if(!usedJ.contains(a)){ usedJ.add(a); a=k+1; } } } if(start == usedJ.size()) { usedJ.add(j); j--; } } else { j--; } } System.out.println(result); } } } |