Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- int main () {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n;
- cin >> n;
- long long a[n + 1];
- for (int i = 1; i <= n; ++i) cin >> a[i];
- sort (a + 1,a + n + 1);
- long long sum = 0;
- //1 based approach
- //lower_bound(a + 1,a + n + 1,x) - (a + 1)
- //0 based approach
- //lower_bound(a,a + n,x) - a;
- for (int i = 1; i <= n; ++i) sum += a[i];
- int m;
- cin >> m;
- for (int i = 1; i <= m; ++i) {
- long long res = LLONG_MAX;
- long long x,y;
- cin >> x >> y; // x = def_power,y = atk power;
- int idx = n;
- int l = 1,r = n;
- while (l <= r) {
- int mid = l + (r - l) / 2;
- if (a[mid] < x) {
- l = mid + 1;
- } else {
- idx = mid;
- r = mid - 1;
- }
- }
- long long p2_attack_need = max(0LL,x - a[idx]); //atk need
- long long p2_def_need = max(0LL,y - (sum - a[idx]));
- res = min(res,p2_attack_need + p2_def_need);
- if (idx != 1) {
- long long p1_attack_need = max(0LL,x - a[idx - 1]);
- long long p1_def_need = max(0LL,y - (sum - a[idx - 1]));
- res = min(res,p1_attack_need + p1_def_need);
- }
- cout << res << '\n';
- }
- }
- Problem Link : https://codeforces.com/contest/1574/problem/C
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement