import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; import java.util.StringTokenizer; import java.lang.StringBuilder; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.HashMap; import java.util.Collection; import java.util.Random; import java.util.TreeSet; import java.util.SortedSet; public class grz{ /*reader template*/ static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine(), " \t\n\r\f/"); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } public static class Field implements Comparable<Field>{ long a; long b; int id; public Field(long a, long b, int id){ this.a = a; this.b = b; this.id = id; } @Override public String toString(){ return "" + a + ", " + b + " -> " + id; } public int compareTo(Field another){ if(a>another.a){ return 1; } if(a == another.a){ if(id > another.id){ return 1; } if(id == another.id){ return 0; } } return -1; } } public static long eval(Collection<Field> fields){ ArrayList<Long> inc = new ArrayList<Long>(); long res = 0; for(Field f : fields){ inc.add(f.a); res += f.b; } Collections.sort(inc); for(long i=0;i<fields.size();++i){ res += inc.get((int)i)*i; } return res; } public static long[] brute(int n, ArrayList<Field> fields){ int maxMask = 1<<n; long results[] = new long[n+1]; for(int i=0;i<maxMask;++i){ int selector = 1; int id = 0; int sampleSize = 0; ArrayList<Field> sample = new ArrayList<Field>(); while(selector < maxMask){ if( (selector&i) > 0){ sample.add(fields.get(id)); } selector<<=1; id ++; } results[sample.size()] = Math.max(results[sample.size()], eval(sample)); } for(int i=1;i<=n;++i){ System.out.println("" + results[i]); } return results; } public static long[] greedy(int n, ArrayList<Field> fields){ HashSet<Field> soFar = new HashSet<Field>(); long results[] = new long[n+1]; for(int i=0;i<n;++i){ int bestInc = -1; for(int j=0;j<n;++j){ if(!soFar.contains(fields.get(j))){ soFar.add(fields.get(j)); long amount = eval(soFar); if(amount > results[i+1]){ bestInc = j; results[i+1] = amount; } soFar.remove(fields.get(j)); } } soFar.add(fields.get(bestInc)); } for(int i=1;i<=n;++i){ System.out.println("" + results[i]); } return results; } public static class Evaluator{ ArrayList<Long> speeds = new ArrayList<Long>(); ArrayList<Long> suff = new ArrayList<Long>(); long result = 0; public long getGoodness(Field f){ if(speeds.size()==0){ return f.b; } int index = Collections.binarySearch(speeds,f.a); // System.out.println(speeds); // System.out.println(suff); // System.out.println("" + f.a + " got id " + index); if(index < 0){ index = (index+1)*(-1); } if(index >= suff.size()){ System.out.println(speeds); System.out.println(suff); System.out.println("" + f.a + " got id " + index); } return suff.get(index) + (index)*f.a + f.b; } public void addNewField(Field f){ result += getGoodness(f); speeds.add(f.a); Collections.sort(speeds); suff = new ArrayList<>(); long sum = 0; for(int i=speeds.size()-1;i>=0;i--){ sum += speeds.get(i); suff.add(sum); } Collections.reverse(suff); suff.add(0L); } } public static long[] fasterGreedy(int n, ArrayList<Field> fields){ HashSet<Integer> notUsed = new HashSet<Integer>(); for(int i=0;i<n;++i){ notUsed.add(i); } long results[] = new long[n+1]; Evaluator ev= new Evaluator(); for(int i=0;i<n;++i){ int bestInc = -1; long max = 0; for(int j : notUsed){ long tmp = ev.getGoodness(fields.get(j)); if(max < tmp){ max = tmp; bestInc = j; } } notUsed.remove(bestInc); ev.addNewField(fields.get(bestInc)); results[i+1] = ev.result; } for(int i=1;i<=n;++i){ System.out.println("" + results[i]); } return results; } public static void another(int n, ArrayList<Field> fields){ TreeSet<Field> notUsed = new TreeSet<Field>(); notUsed.addAll(fields); long result = 0L; long c = 0; Field max = new Field(0,0,0); int i=1; PrintWriter pw = new PrintWriter(System.out); while(notUsed.size()>0){ long maxGoodness = -1; for(Field cand : notUsed){ if(maxGoodness < cand.b){ maxGoodness = cand.b; max = cand; } } notUsed.remove(max); SortedSet<Field> tail = notUsed.tailSet(max); for(Field cand : tail){ cand.b += cand.a- max.a; } result += maxGoodness+c; c+=max.a; pw.println("" + result); i++; } pw.flush(); pw.close(); } public static void solve(int n, ArrayList<Field> fields){ //brute(n,fields); //greedy(n, fields); //fasterGreedy(n, fields); another(n, fields); } public static void main(String args[]){ InputReader reader = new InputReader(System.in); int n = reader.nextInt(); ArrayList<Field> fields = new ArrayList<Field>(n); for(int i=0; i<n; ++i){ long a = reader.nextLong(); long b = reader.nextLong(); fields.add(new Field(a,b,i+1)); } solve(n, fields); } }
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; import java.util.StringTokenizer; import java.lang.StringBuilder; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.HashMap; import java.util.Collection; import java.util.Random; import java.util.TreeSet; import java.util.SortedSet; public class grz{ /*reader template*/ static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine(), " \t\n\r\f/"); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } public static class Field implements Comparable<Field>{ long a; long b; int id; public Field(long a, long b, int id){ this.a = a; this.b = b; this.id = id; } @Override public String toString(){ return "" + a + ", " + b + " -> " + id; } public int compareTo(Field another){ if(a>another.a){ return 1; } if(a == another.a){ if(id > another.id){ return 1; } if(id == another.id){ return 0; } } return -1; } } public static long eval(Collection<Field> fields){ ArrayList<Long> inc = new ArrayList<Long>(); long res = 0; for(Field f : fields){ inc.add(f.a); res += f.b; } Collections.sort(inc); for(long i=0;i<fields.size();++i){ res += inc.get((int)i)*i; } return res; } public static long[] brute(int n, ArrayList<Field> fields){ int maxMask = 1<<n; long results[] = new long[n+1]; for(int i=0;i<maxMask;++i){ int selector = 1; int id = 0; int sampleSize = 0; ArrayList<Field> sample = new ArrayList<Field>(); while(selector < maxMask){ if( (selector&i) > 0){ sample.add(fields.get(id)); } selector<<=1; id ++; } results[sample.size()] = Math.max(results[sample.size()], eval(sample)); } for(int i=1;i<=n;++i){ System.out.println("" + results[i]); } return results; } public static long[] greedy(int n, ArrayList<Field> fields){ HashSet<Field> soFar = new HashSet<Field>(); long results[] = new long[n+1]; for(int i=0;i<n;++i){ int bestInc = -1; for(int j=0;j<n;++j){ if(!soFar.contains(fields.get(j))){ soFar.add(fields.get(j)); long amount = eval(soFar); if(amount > results[i+1]){ bestInc = j; results[i+1] = amount; } soFar.remove(fields.get(j)); } } soFar.add(fields.get(bestInc)); } for(int i=1;i<=n;++i){ System.out.println("" + results[i]); } return results; } public static class Evaluator{ ArrayList<Long> speeds = new ArrayList<Long>(); ArrayList<Long> suff = new ArrayList<Long>(); long result = 0; public long getGoodness(Field f){ if(speeds.size()==0){ return f.b; } int index = Collections.binarySearch(speeds,f.a); // System.out.println(speeds); // System.out.println(suff); // System.out.println("" + f.a + " got id " + index); if(index < 0){ index = (index+1)*(-1); } if(index >= suff.size()){ System.out.println(speeds); System.out.println(suff); System.out.println("" + f.a + " got id " + index); } return suff.get(index) + (index)*f.a + f.b; } public void addNewField(Field f){ result += getGoodness(f); speeds.add(f.a); Collections.sort(speeds); suff = new ArrayList<>(); long sum = 0; for(int i=speeds.size()-1;i>=0;i--){ sum += speeds.get(i); suff.add(sum); } Collections.reverse(suff); suff.add(0L); } } public static long[] fasterGreedy(int n, ArrayList<Field> fields){ HashSet<Integer> notUsed = new HashSet<Integer>(); for(int i=0;i<n;++i){ notUsed.add(i); } long results[] = new long[n+1]; Evaluator ev= new Evaluator(); for(int i=0;i<n;++i){ int bestInc = -1; long max = 0; for(int j : notUsed){ long tmp = ev.getGoodness(fields.get(j)); if(max < tmp){ max = tmp; bestInc = j; } } notUsed.remove(bestInc); ev.addNewField(fields.get(bestInc)); results[i+1] = ev.result; } for(int i=1;i<=n;++i){ System.out.println("" + results[i]); } return results; } public static void another(int n, ArrayList<Field> fields){ TreeSet<Field> notUsed = new TreeSet<Field>(); notUsed.addAll(fields); long result = 0L; long c = 0; Field max = new Field(0,0,0); int i=1; PrintWriter pw = new PrintWriter(System.out); while(notUsed.size()>0){ long maxGoodness = -1; for(Field cand : notUsed){ if(maxGoodness < cand.b){ maxGoodness = cand.b; max = cand; } } notUsed.remove(max); SortedSet<Field> tail = notUsed.tailSet(max); for(Field cand : tail){ cand.b += cand.a- max.a; } result += maxGoodness+c; c+=max.a; pw.println("" + result); i++; } pw.flush(); pw.close(); } public static void solve(int n, ArrayList<Field> fields){ //brute(n,fields); //greedy(n, fields); //fasterGreedy(n, fields); another(n, fields); } public static void main(String args[]){ InputReader reader = new InputReader(System.in); int n = reader.nextInt(); ArrayList<Field> fields = new ArrayList<Field>(n); for(int i=0; i<n; ++i){ long a = reader.nextLong(); long b = reader.nextLong(); fields.add(new Field(a,b,i+1)); } solve(n, fields); } } |