malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   void input() {     freopen( give_me.txt ,  r , stdin);     freopen( see_it.txt ,  w , stdout);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long n, k;     cin >> n >> k;     long long a[n], sum = 0, w = n - k + 1;     for (int i = 0; i < n; i++) cin >> a[i];     long long gun[n], cur = 1, mx = k;     if (k > n / 2) mx = (n - k) + 1;     if (n % 2 == 0) {       for (int i = 0; i < n / 2; i++) {         gun[i] = cur;         gun[n - i - 1] = cur;         if (cur <= mx - 1) cur++;       }     } else {       for (int i = 0; i <= n / 2; i++) {         gun[i] = cur;         gun[n - i - 1] = cur;         if (cur <= k - 1) cur++;       }     }     for (int i = 0; i < n; i++) {       sum += (a[i] * gun[i]);     }     double ans = (1.0 * sum / w);     printf( %.6lf , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long double eps = 1e-12;   const int maxn = 100000 + 1912;   const int MX = 1e6;   int n;   pair<int, int> a[maxn];   vector<int> p[MX * 2 + 3];   long long res = 0;   void ReadData() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &a[i].first, &a[i].second);       a[i].first += MX;       a[i].second += MX;       p[a[i].first].push_back(a[i].second);     }     for (int i = 0; i <= MX * 2; i++)       if (((int)p[i].size())) sort(p[i].begin(), p[i].end());   }   bool Found(int x, int y) {     if (x < 0 || x > MX * 2) return false;     vector<int>::iterator it = lower_bound(p[x].begin(), p[x].end(), y);     if (it != p[x].end() && (*it) == y) return true;     return false;   }   void Process() {     for (int i = 0; i <= MX * 2; i++)       if (((int)p[i].size())) {         if (((int)p[i].size()) <= 520) {           for (int fi = 0; fi < ((int)p[i].size()); fi++)             for (int se = 0; se < fi; se++) {               int len = p[i][fi] - p[i][se];               if (i >= len && Found(i - len, p[i][se]) &&                   Found(i - len, p[i][fi]))                 res++;             }         } else {           for (int j = 0; j <= i - 1; j++) {             int len = i - j;             for (int k = 0; k < ((int)p[j].size()); k++) {               if (Found(i, p[j][k]) && Found(j, p[j][k] + len) &&                   Found(i, p[j][k] + len))                 res++;             }           }         }       }     cout << res << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ReadData();     Process();   }
#include <bits/stdc++.h>   using namespace std;   void setIO(string name) {     freopen((name +  .in ).c_str(),  r , stdin);     freopen((name +  .out ).c_str(),  w , stdout);   }   long long inv(long long base, long long mod = 998244353) {     if (base == 0) return mod;     long long res = (1 - mod * inv(mod % base, base)) / base;     return (res < 0) ? res + mod : res;   }   int N;   int c[300001];   long long pref[300001];   long long mod = 998244353ll;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> N;     for (int i = 1; i < N + 1; i++) cin >> c[i];     sort(c + 1, c + N + 1, greater<int>());     for (int i = 1; i < N + 1; i++) pref[i] = (pref[i - 1] + c[i]) % mod;     for (int k = 1; k < N + 1; k++) {       long long score = 0;       long long i;       for (i = k; i <= N; i += k) {         score += (pref[i] - pref[i - k] + mod) * (i / k - 1) % mod;         if (score > mod) score -= mod;       }       score +=           (i + k > N) ? (pref[N] - pref[i - k] + mod) * (i / k - 1) % mod : 0;       if (score > mod) score -= mod;       score *= inv(N);       score %= mod;       cout << score <<    ;     }     return 0;   }
#include <bits/stdc++.h>   int main() {     int n, i, j, r;     char lvl[110];     scanf( %d , &n);     scanf(  %s , lvl);     for (r = 1; r < n; r++) {       for (i = 0; i < n; i++) {         for (j = 0; j <= 4; j++) {           if (i + j * r >= n || lvl[i + j * r] !=  * ) break;         }         if (j >= 5) {           printf( yes n );           return 0;         }       }     }     printf( no n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int x, y;     cin >> x >> y;     int m = 0;     while (x * 100 + y * 10 >= 220) {       int kx, ky;       if (m % 2 == 0) {         kx = min(2, x);         x -= kx;         ky = (220 - 100 * kx) / 10;         y -= ky;         if (y < 0) break;       } else {         if (y >= 22)           ky = 22;         else if (y >= 12)           ky = 12;         else           ky = 2;         y -= ky;         if (y < 0) break;         kx = (220 - ky * 10) / 100;         x -= kx;         if (x < 0) break;       }       m++;     }     if (m % 2 == 1) {       cout <<  Ciel n ;     } else       cout <<  Hanako n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2000 + 10;   const int maxm = 4e5 + 10;   const long long mod = 1e9 + 7;   const int inf = 0x3f3f3f3f;   const double eps = 1e-5;   const double pi = acos(-1.0);   int t;   int n, a[maxn];   int cnt[maxn];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> t;     while (t--) {       cin >> n;       vector<int> res;       for (int i = 0; i <= n; i++) cnt[i] = 0;       for (int i = 0; i < n; i++) {         cin >> a[i];         cnt[a[i]]++;       }       int now;       while (1) {         now = 0;         bool flag = false;         for (int i = 1; i < n; i++)           if (a[i] < a[i - 1]) flag = true;         if (flag == false) break;         for (int i = 0; i < n; i++) {           if (cnt[i])             now = i + 1;           else             break;         }         if (now == n) {           int index = 0, minnum = maxn;           for (int i = 0; i < n; i++)             if (a[i] != i) {               if (a[i] < minnum) {                 index = i;                 minnum = a[i];               }             }           int p = a[index];           cnt[p]--;           cnt[now]++;           a[index] = now;           res.push_back(index);           continue;         }         int p = a[now];         cnt[p]--;         cnt[now]++;         a[now] = now;         res.push_back(now);       }       cout << res.size() << endl;       for (int i = 0; i < res.size(); i++) cout << res[i] + 1 <<    ;       cout << endl;     }     return ~~(0 - 0);   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1 << 17;   int st[N], op[N], sz[N], dp[N], pid[N], c[N], hd[N], p[N], f[N], g[N], add[N];   double ans[N];   vector<int> adj[N], V[N];   long long sum[N];   int T, pn;   void dfs(int u) {     sz[u] = 1;     dp[u] = dp[p[u]] + 1;     for (auto v : adj[u]) {       dfs(v);       sz[u] += sz[v];     }   }   void dfs1(int u) {     st[u] = ++T;     pid[u] = pn;     if (!hd[pn]) hd[pn] = u;     int w = 0;     for (auto v : adj[u])       if (sz[v] > sz[w]) w = v;     if (w) dfs1(w);     for (auto v : adj[u]) {       if (v != w) {         pn++;         dfs1(v);       }     }   }   long long ret2, ret1;   void renew(int u, int l, int k) {     add[u] += k;     sum[u] += k * l;   }   void push_down(int u, int l) {     if (add[u]) {       renew(u + u, (l + 1) / 2, add[u]);       renew(u + u + 1, l / 2, add[u]);       add[u] = 0;     }   }   void push_up(int u) { sum[u] = sum[u + u] + sum[u + u + 1]; }   void modify(int u, int l, int r, int ll, int rr, int k) {     if (ll <= l && r <= rr) {       renew(u, r - l + 1, k);       return;     }     int mid = l + r >> 1;     push_down(u, r - l + 1);     if (ll <= mid) modify(u + u, l, mid, ll, rr, k);     if (mid < rr) modify(u + u + 1, mid + 1, r, ll, rr, k);     push_up(u);   }   long long query(int u, int l, int r, int ll, int rr) {     if (ll <= l && r <= rr) return sum[u];     int mid = l + r >> 1;     long long ret = 0;     push_down(u, r - l + 1);     if (ll <= mid) ret += query(u + u, l, mid, ll, rr);     if (mid < rr) ret += query(u + u + 1, mid + 1, r, ll, rr);     return ret;   }   int n;   void update(int u, int k) {     ret1 += k * dp[u];     ret2 += dp[u];     while (u) {       int l = hd[pid[u]];       ret2 += 2 * k * query(1, 1, n, st[l], st[u]);       modify(1, 1, n, st[l], st[u], k);       u = p[l];     }   }   int main() {     int m, q, C;     scanf( %d%d%d%d , &n, &m, &q, &C);     for (int i = 1; i <= n; i++)       scanf( %d , &f[i]), V[f[i]].push_back(i), g[i] = f[i];     for (int i = 2; i <= n; i++) scanf( %d , &p[i]), adj[p[i]].push_back(i);     for (int i = 1; i <= m; i++) scanf( %d , &c[i]);     for (int i = 1; i <= q; i++) {       scanf( %d , &op[i]);       if (op[i] == 1) {         int x, w;         scanf( %d%d , &x, &w);         V[g[x]].push_back(-x);         g[x] = w;         V[g[x]].push_back(x);       } else {         int k;         scanf( %d , &k);         V[k].push_back(n + i);       }     }     dfs(1);     dfs1(1);     set<int> S;     for (int i = 1; i <= m; i++) {       for (auto e : S) update(e, -1);       S.clear();       ret2 = 0, ret1 = 0;       for (int event : V[i]) {         if (event < 0) {           S.erase(-event);           update(-event, -1);         } else if (event <= n) {           S.insert(event);           update(event, 1);         } else {           ans[event - n] = (long double)(ret2 * c[i] * c[i] -                                          2 * ret1 * c[i] * C + C * 1LL * C * n) /                            n;         }       }     }     for (int i = 1; i <= q; i++)       if (op[i] == 2) printf( %.10lf n , ans[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     std::ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int n, even = 0, odd = 0;     cin >> n;     long long int a[n];     for (long long int i = 0; i < n; i++) cin >> a[i];     if (n == 1)       cout << a[0] <<   n ;     else {       sort(a, a + n);       long long int flag = 0;       for (long long int i = 1; i < n; i++) {         if (a[i] % a[0] != 0) {           flag = 1;           break;         }       }       if (flag == 0)         cout << a[0] <<   n ;       else         cout <<  -1               <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   void _print(long long int t) { cerr << t; }   void _print(int t) { cerr << t; }   void _print(string t) { cerr << t; }   void _print(char t) { cerr << t; }   void _print(long double t) { cerr << t; }   void _print(double t) { cerr << t; }   void _print(unsigned long long t) { cerr << t; }   template <class T, class V>   void _print(pair<T, V> p);   template <class T>   void _print(vector<T> v);   template <class T>   void _print(set<T> v);   template <class T, class V>   void _print(map<T, V> v);   template <class T>   void _print(multiset<T> v);   template <class T, class V>   void _print(pair<T, V> p) {     cerr <<  { ;     _print(p.first);     cerr <<  , ;     _print(p.second);     cerr <<  } ;   }   template <class T>   void _print(vector<T> v) {     cerr <<  [  ;     for (T i : v) {       _print(i);       cerr <<    ;     }     cerr <<  ] ;   }   template <class T>   void _print(set<T> v) {     cerr <<  [  ;     for (T i : v) {       _print(i);       cerr <<    ;     }     cerr <<  ] ;   }   template <class T>   void _print(multiset<T> v) {     cerr <<  [  ;     for (T i : v) {       _print(i);       cerr <<    ;     }     cerr <<  ] ;   }   template <class T, class V>   void _print(map<T, V> v) {     cerr <<  [  ;     for (auto i : v) {       _print(i);       cerr <<    ;     }     cerr <<  ] ;   }   long long int mod1 = 1e9 + 7;   long long int INF = 1e9 + 5;   long long int mod = 998244353;   void solve(long long int test_case) {     string s;     cin >> s;     long long int n = s.size();     long long int take = n / 2;     string ans, rev;     long long int i = 0, j = n - 1;     while (j >= i && take > 0) {       if (j > i && s[i] == s[j]) {         ans.push_back(s[i]);         rev.push_back(s[i]);         i++;         j--;         take -= 2;       } else if (j - 1 > i && s[i] == s[j - 1]) {         ans.push_back(s[i]);         rev.push_back(s[i]);         j -= 2;         i++;         take -= 2;       } else if (j > i + 1 && s[i + 1] == s[j]) {         ans.push_back(s[j]);         rev.push_back(s[j]);         j--;         i += 2;         take -= 2;       } else if (j - 1 > i + 1 && s[j - 1] == s[i + 1]) {         ans.push_back(s[j - 1]);         rev.push_back(s[j - 1]);         j -= 2;         i += 2;         take -= 2;       } else {         ans.push_back(s[i]);         i++;         take--;         break;       }     }     reverse(rev.begin(), rev.end());     ans += rev;     if (take > 0 || ans.size() < n / 2) {       cout <<  IMPOSSIBLE n ;     } else {       cout << ans;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long int t = 1;     long long int i = 1ll;     while (t--) {       solve(i);       i++;     }   }
#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   template <class T>   T SQR(const T &a) {     return a * a;   }   void run() {     int A[4][4];     int n;     cin >> n;     int B[16];     for (int i = (0), ei = (n * n); i < ei; i++) scanf( %d , B + i);     int nsq = n * n;     sort(B, B + nsq);     int S[4], G[4];     do {       memset(S, 0, 16);       for (int i = (0), ei = (n); i < ei; i++)         for (int j = (0), ej = (n); j < ej; j++) S[i] += B[i * n + j];       bool ok = 1;       for (int i = (1), ei = (n); i < ei; i++)         if (S[i] != S[0]) ok = 0;       if (!ok) continue;       memset(G, 0, 16);       for (int j = (0), ej = (n); j < ej; j++)         for (int i = (0), ei = (n); i < ei; i++) G[j] += B[i * n + j];       if (S[0] != G[0]) continue;       for (int i = (1), ei = (n); i < ei; i++)         if (G[i] != G[0]) ok = 0;       int d = 0;       for (int i = (0), ei = (n); i < ei; i++) d += B[i * n + i];       if (d != S[0]) continue;       d = 0;       for (int i = (0), ei = (n); i < ei; i++) d += B[i * n + n - i - 1];       if (d != S[0]) continue;       if (ok) {         printf( %d n , S[0]);         for (int i = (0), ei = (n); i < ei; i++)           for (int j = (0), ej = (n); j < ej; j++) {             printf( %d , B[i * n + j]);             if (j != n - 1)               putchar(   );             else               putchar(  n );           }         return;       }     } while (next_permutation(B, B + nsq));   }   int main() {     time_t beg = clock();     run();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e5 + 10;   long long n;   vector<long long> v;   void init() {     for (int i = 0; i <= 38; i++) v.push_back(pow(3, i));   }   int main() {     init();     int t;     cin >> t;     while (t--) {       cin >> n;       long long res = 0, cur = 0;       while (res < n) {         res += v[cur];         cur++;       }       while (cur >= 0) {         if (res - v[cur] >= n) res -= v[cur];         cur--;       }       cout << res << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   T gcd(T a, T b) {     return b ? gcd(b, a % b) : a;   }   const int maxn = 1000100;   char s[maxn];   vector<int> a[maxn];   void solve() {     cin >> s;     int n = strlen(s);     int c = 0;     for (int i = 0; i < n; i++) {       c += s[i] ==  0  ? 1 : -1;       if (c < 0) {         puts( -1 );         return;       }     }     c = 0;     for (int i = n - 1; i >= 0; i--) {       c += s[i] ==  0  ? 1 : -1;       if (c < 0) {         puts( -1 );         return;       }     }     int nol = 0, bir = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  0 ) {         if (bir) {           a[bir - 1].push_back(i + 1);           bir--;         } else {           nol++;           a[nol - 1].push_back(i + 1);         }       } else {         a[bir].push_back(i + 1);         bir++;       }     }     cout << nol << endl;     for (int i = 0; i < nol; i++) {       printf( %d , a[i].size());       for (int j = 0; j < a[i].size(); j++) printf(  %d , a[i][j]);       cout << endl;     }   }   int main() {     int t = 1;     while (t--) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c;     scanf( %d %d %d , &a, &b, &c);     double rez = 1.0 * a * a * a * 1.41421356237309 / 12.0;     rez += 1.0 * b * b * b * 1.41421356237309 / 6.0;     rez += 1.0 * c * c * c * 7.23606797749978 / 24.0;     printf( %.10lf n , rez);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MX = 5005;   int n, m;   int n1, n2, n3;   vector<int> g[5005];   int pars[5005];   map<int, vector<int>> roots;   vector<int> roots_v;   vector<int> cs;   int rootNum = 0;   bool dp[5005][5005][2];   int find(int u) {     if (pars[u] == u) return u;     int p = find(pars[u]);     pars[u] = p;     return p;   }   void un(int u, int v) {     int pu = find(u), pv = find(v);     pars[pv] = pu;   }   bool color(int u, int c) {     stack<vector<int>> sk;     sk.push({u, c});     while (!sk.empty()) {       vector<int> it = sk.top();       sk.pop();       u = it[0];       c = it[1];       if (cs[u] != 0 && cs[u] != c) return false;       if (cs[u] != 0) continue;       int pu = find(u);       cs[u] = c;       roots[pu][c]++;       int nc = c == 1 ? 2 : 1;       for (int v : g[u]) {         if (cs[v] == 0) sk.push({v, nc});       }     }     return true;   }   void solve() {     for (int u = 1; u <= n; u++) {       pars[u] = u;     }     for (int u = 1; u <= n; u++) {       for (int v : g[u]) {         un(u, v);       }     }     for (int u = 1; u <= n; u++) {       int pu = find(u);       roots[pu] = vector<int>(3);     }     cs = vector<int>(n + 1, 0);     for (auto it = roots.begin(); it != roots.end(); it++) {       int u = it->first;       roots_v.push_back(u);       if (!color(u, 1)) {         cout <<  NO  << endl;         return;       }     }     rootNum = roots.size();     memset(dp, 0, sizeof(dp));     int root = roots_v[0];     if (roots[root][2] <= n2) dp[0][roots[root][2]][0] = 1;     if (roots[root][1] <= n2) dp[0][roots[root][1]][1] = 1;     for (int i = 1; i < rootNum; i++) {       root = roots_v[i];       int c1 = roots[root][1], c2 = roots[root][2];       for (int v = 0; v <= n2; v++) {         if (v >= c2) {           dp[i][v][0] = dp[i - 1][v - c2][0] || dp[i - 1][v - c2][1];         }         if (v >= c1) {           dp[i][v][1] = dp[i - 1][v - c1][0] || dp[i - 1][v - c1][1];         }       }     }     if (!dp[rootNum - 1][n2][0] && !dp[rootNum - 1][n2][1]) {       cout <<  NO  << endl;       return;     }     int i = rootNum - 1;     int tar = n2;     map<int, int> reverse;     while (i >= 0) {       int root = roots_v[i];       if (dp[i][tar][1]) {         reverse[root] = 1;         tar -= roots[root][1];       } else {         reverse[root] = 0;         tar -= roots[root][2];       }       i--;     }     for (int u = 1; u <= n; u++) {       int pu = find(u);       if (reverse[pu]) {         cs[u] = cs[u] == 1 ? 2 : 1;       }     }     int x = n3;     for (int u = 1; u <= n && x; u++) {       if (cs[u] == 1) {         cs[u] = 3;         x--;       }     }     cout <<  YES  << endl;     for (int u = 1; u <= n; u++) {       cout << cs[u];     }     cout << endl;   }   int main() {     cin >> n >> m >> n1 >> n2 >> n3;     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long begtime = clock();   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cout << name <<   :   << arg1 <<   n ;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cout.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   void splitx(string s, char c, vector<string>& v) {     long long i = 0, j;     while (i < (long long)(s).size()) {       j = i;       string temp;       while (j < (long long)(s).size() && s[j] != c) {         temp += s[j];         j++;       }       v.push_back(temp);       i = j + 1;     }   }   vector<long long> dx{1, 0, -1, 0, 1, 1, -1, -1};   vector<long long> dy{0, 1, 0, -1, 1, -1, 1, -1};   signed main() {     std::ios::sync_with_stdio(false);     cin.tie(NULL);     long long n, m, k;     cin >> n >> m >> k;     vector<string> v(n);     for (long long i = 0; i < n; i++) {       cin >> v[i];     }     vector<vector<long long> > vis(n, vector<long long>(m, 0));     vector<vector<long long> > dist(n, vector<long long>(m, 1000000000000000007));     long long x1, y1, x2, y2;     cin >> x1 >> y1 >> x2 >> y2;     x1--;     y1--;     x2--;     y2--;     queue<pair<long long, long long> > q;     q.push({x1, y1});     dist[x1][y1] = 0;     while (!q.empty()) {       pair<long long, long long> p = q.front();       q.pop();       long long x = p.first;       long long y = p.second;       if (vis[x][y]) continue;       vis[x][y] = 1;       for (long long i = 0; i < 4; i++) {         for (long long j = 1; j < k + 1; j++) {           long long nx = x + dx[i] * j;           long long ny = y + dy[i] * j;           if (nx < 0 || nx >= n || ny < 0 || ny >= m) break;           if (v[nx][ny] ==  #  || dist[nx][ny] < dist[x][y] + 1) break;           dist[nx][ny] = dist[x][y] + 1;           q.push({nx, ny});         }       }     }     if (dist[x2][y2] == 1000000000000000007) {       cout << -1 <<   n ;     } else {       cout << dist[x2][y2] <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 203;   const int mod = 1e9 + 7;   vector<int> g[N];   int dp[N];   int ans, n;   int len1, len2;   void cal(int u, int fa, int &len) {     dp[u] = 0;     int &mx1 = dp[u], mx2 = 0;     int v;     for (__typeof((g[u]).begin()) v = (g[u]).begin(); v != (g[u]).end(); v++) {       if (*v == fa) continue;       cal(*v, u, len);       if (mx2 < dp[*v] + 1) {         mx2 = dp[*v] + 1;         if (mx2 > mx1) swap(mx1, mx2);       }     }     len = max(len, mx1 + mx2);   }   void dfs(int u, int fa) {     int v;     for (__typeof((g[u]).begin()) v = (g[u]).begin(); v != (g[u]).end(); v++) {       if (*v == fa) continue;       len1 = len2 = 0;       cal(*v, u, len1);       cal(u, *v, len2);       ans = max(ans, len1 * len2);       dfs(*v, u);     }   }   int main() {     int i, j;     scanf( %d , &n);     for (i = 1; i < n; i++) {       int x, y;       scanf( %d%d , &x, &y);       g[x].push_back(y);       g[y].push_back(x);     }     dfs(1, 0);     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, k;   long long a[200010];   long long b[200010];   int s[200010];   long long ans;   bool check(long long val) {     memset(s, 0, sizeof(s));     int num = k;     for (int i = 1; i <= n; i++) {       if (!b[i]) {         continue;       }       long long sum = a[i];       s[min(sum / b[i] + 1, 1ll * (k + 1))]++;       while (sum < 1ll * k * b[i]) {         if (!num) {           break;         }         sum += val;         s[min(sum / b[i] + 1, 1ll * (k + 1))]++;         num--;       }     }     for (int i = 1; i <= k; i++) {       s[i] += s[i - 1];       if (s[i] > i) {         return false;       }     }     return true;   }   int main() {     scanf( %d%d , &n, &k);     k--;     for (int i = 1; i <= n; i++) {       scanf( %lld , &a[i]);     }     for (int i = 1; i <= n; i++) {       scanf( %lld , &b[i]);     }     long long l = 0, r = 2000000000000ll;     while (l <= r) {       long long mid = (l + r) >> 1;       if (check(mid)) {         ans = mid;         r = mid - 1;       } else {         l = mid + 1;       }     }     if (!ans) {       printf(check(0) ?  0  :  -1 );     } else {       printf( %lld , ans);     }   }