fork(4) download
  1. #include <bits/stdc++.h>
  2. #define pi 3.14159265359
  3. using namespace std;
  4.  
  5. int main() {
  6. int N;
  7. long long M;
  8. cin >> N >> M;
  9. if(M == 0) {cout << "0\n"; return 0;}
  10. vector<long long> A(N),B(N);
  11. for(int i =0; i < N; i++) cin >> A[i];
  12. for(int i =0; i < N; i++) cin >> B[i];
  13. long long Cmin =0,Cmax =1000000000+42;
  14. while(Cmax-Cmin > 1) {
  15. long long C =(Cmin+Cmax)/2, m =0;
  16. for(int i =0; i < N; i++) if(A[i] >= C) {
  17. long long k =(A[i]-C)/B[i]+1;
  18. m +=k;}
  19. if(m < M) Cmax =C;
  20. else Cmin =C;}
  21. long long ans =0, m =0;
  22. for(int i =0; i < N; i++) if(A[i] >= Cmax) {
  23. long long k =(A[i]-Cmax)/B[i]+1;
  24. m +=k;
  25. ans +=1LL*k*A[i]-B[i]*1LL*k*(k-1)/2;}
  26. ans +=(M-m)*max(1LL,Cmin);
  27. cout << ans << "\n";
  28. return 0;}
Success #stdin #stdout 0s 3436KB
stdin
3 7
9 7 17
2 1 4
stdout
68