publicstaticvoidsolve() { intn= io.nextInt(), m = io.nextInt(); int[] a = newint[n]; for (inti=1; i < n; i++) { a[i] = io.nextInt(); } int[] b = newint[n]; for (inti=0; i < n; i++) { b[i] = io.nextInt(); } Arrays.sort(b);
intk= calc(a, b, 1);
intlo=1, hi = m; while (lo <= hi) { intmid= lo + (hi - lo) / 2; if (calc(a, b, mid) == k) lo = mid + 1; else hi = mid - 1; } io.println((long) m * k + (m - lo + 1)); }
privatestaticintcalc(int[] a, int[] b, int x) { a[0] = x; a = a.clone(); Arrays.sort(a);
intn= a.length, k = 0; for (inti=0, j = 0; i < n - k; i++, j++) { while (j < n && a[i] >= b[j]) { k++; j++; } } return k; }
publicstaticvoidsolve() { intn= io.nextInt(), m = io.nextInt(); int[] a = newint[n]; a[0] = Integer.MAX_VALUE; for (inti=1; i < n; i++) { a[i] = io.nextInt(); } int[] b = newint[n]; for (inti=0; i < n; i++) { b[i] = io.nextInt(); }
Arrays.sort(a); Arrays.sort(b);
intk=0, val = m + 1; for (inti=0, j = 0; i < n - k; i++, j++) { while (j < n && a[i] >= b[j]) { val = b[j]; k++; j++; } } io.println((long) m * (k - 1) + Math.max(0, m - val + 1)); }