This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
long M = Long.parseLong(st.nextToken());
long [] a, b;
a = new long[N];
b = new long[N];
st = new StringTokenizer(br.readLine());
StringTokenizer st1 = new StringTokenizer(br.readLine());
for (int i=0;i<N;i++) {
a[i] = Long.parseLong(st.nextToken());
b[i] = Long.parseLong(st1.nextToken());
}
pw.println(bs(a, b, N, M));
pw.close();
br.close();
}
public static boolean f(long [] a, long [] b, int N, long M, long mid) {
long ans = 0;
for (int i=0;i<a.length;i++) {
if (a[i]>b[i]) {
long nC = Math.min(M, Math.max((mid+a[i]-1)/a[i], 1));
//long nC = Math.min(M, 1);
ans+=nC;
long remain = mid-nC*a[i];
if (remain>0) {
ans+=(remain+b[i]-1)/b[i];
}
} else {
ans+=(mid+b[i]-1)/b[i];
}
}
return ans<N*M;
}
public static long bs(long [] a, long [] b, int N, long M) {
long lo = 1;
long hi = (long)Math.pow(10,18);
while (lo<hi) {
long mid = (lo+hi+1)/2;
if (f(a, b, N, M, mid)) {
lo = mid;
} else {
hi = Math.max(mid-1, 1);
}
}
return lo;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |