Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define F first
- #define S second
- #define all(x) x.begin(),x.end()
- #define endl '\n'
- using namespace std;
- using ll = long long;
- using pii = pair<int, int>;
- const int INF = 0x3f3f3f3f;
- const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
- const int MOD = 1000000007;
- const int dx[] = { 0, 0, -1, 1, 1, -1, 1, -1};
- const int dy[] = {-1, 1, 0, 0, 1, -1, -1, 1};
- ll AP(ll a, ll b){
- return ((a+b)*(b-a+1))/2;
- }
- const int MAXN = 200010;
- int a[MAXN], b[MAXN];
- int n, m;
- ll f(ll x){
- ll sum = 0;
- for(int i=0; i<n; i++){
- ll t = (a[i]-x)/b[i];
- if(x <= a[i])
- sum += (t+1);
- }
- return sum;
- }
- int main() {
- ios_base::sync_with_stdio(false); cin.tie(NULL);
- cin >> n >> m;
- for(int i=0; i<n; i++){
- cin >> a[i];
- }
- for(int i=0; i<n; i++){
- cin >> b[i];
- }
- ll lo=2, hi=1e9, mn=1;
- while(lo<=hi){
- ll mid = (lo+hi)/2;
- if(f(mid) >= m){
- mn = mid;
- lo = mid+1;
- }else{
- hi = mid-1;
- }
- }
- ll sum = 0;
- for(int i=0; i<n; i++){
- ll t = (a[i]-mn)/b[i];
- if(mn <= a[i]){
- sum += a[i]*(t+1) - b[i]*AP(0, t);
- }
- }
- ll get = f(mn);
- if(mn == 1){
- get += m;
- sum += m;
- }
- if(get >= m){
- sum -= (get-m)*mn;
- }
- cout << sum << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement