fork(2) download
  1. import java.util.*;
  2.  
  3. public class Main {
  4.  
  5. private static class Pair{
  6. int a, b;
  7.  
  8. public Pair(int a, int b) {
  9. this.a=a;
  10. this.b=b;
  11. }
  12.  
  13. }
  14.  
  15. public static void main (String[] args) {
  16. Scanner scanner = new Scanner(System.in);
  17. int n = scanner.nextInt();
  18. int na = scanner.nextInt();
  19. int[] A = new int[na];
  20. int[] times = new int[n];
  21. PriorityQueue<Pair> q = new PriorityQueue<>((a, b)->a.a-b.a);
  22. for(int i=0; i<na; i++) {
  23. A[i] = scanner.nextInt();
  24. q.add(new Pair(A[i], A[i]));
  25. }
  26. for(int i=0; i<n; i++) {
  27. Pair pair=q.poll();
  28. times[i]=pair.a;
  29. q.add(new Pair(pair.a+pair.b, pair.b));
  30. }
  31. q.clear();
  32. int nb = scanner.nextInt();
  33. int[] B = new int[nb];
  34. int max_time=0;
  35. for(int i=0; i<nb; i++) {
  36. B[i] = scanner.nextInt();
  37. q.add(new Pair(B[i], B[i]));
  38. }
  39. for(int i=n-1; i>=0; i--) {
  40. Pair pair=q.poll();
  41. times[i]+=pair.a;
  42. q.add(new Pair(pair.a+pair.b, pair.b));
  43. if(times[i]>max_time)
  44. max_time=times[i];
  45. }
  46. System.out.println(max_time);
  47. }
  48. }
Success #stdin #stdout 0.15s 2184192KB
stdin
6
3
1 3 2
2
2 3
stdout
9