fork(1) download
  1. import java.util.*;
  2.  
  3. class E_LetsGoRolling {
  4. public static void main(String[] args) {
  5. Scanner s = new Scanner(System.in);
  6. int n = s.nextInt();
  7. long[][] marbles = new long[n][2];
  8. for (int i = 0; i < n; ++i) {
  9. marbles[i][0] = s.nextLong();
  10. marbles[i][1] = s.nextLong();
  11. }
  12. Arrays.sort(marbles, new Comparator<long[]>() {
  13. public int compare(long[] o1, long[] o2) {
  14. return (int) (o1[0] - o2[0]);
  15. }
  16. });
  17. long[] cost = new long[n+1];
  18. for (int i = n; i-- > 0;) {
  19. long sum = 0;
  20. cost[i] = Long.MAX_VALUE;
  21. for(int j = i; j < n; ++j){
  22. sum += marbles[j][0]-marbles[i][0];
  23. cost[i] = Math.min(cost[i], cost[j+1] + sum + marbles[i][1]);
  24. }
  25. }
  26. System.out.println(cost[0]);
  27. }
  28. }
  29.  
Success #stdin #stdout 0.07s 213888KB
stdin
3
2 3
3 4
1 2
stdout
5