fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4. using pii = pair<int,int>;
  5. using pll = pair<ll,ll>;
  6. #define FOR(i,a,b) for(ll i = (a);i <= (b);i++)
  7. #define FOD(i,a,b) for(ll i = (a);i >= (b);i--)
  8. #define el '\n'
  9. #define ft first
  10. #define sc second
  11. const ll mang = 1e5 + 5;
  12. const ll MOD = 1e9 + 7;
  13. const ll MAX = 1e18;
  14. const ll MAXMASK = (1 << 18);
  15. ll n,m,d,q,k,k2;
  16. vector<ll> a;
  17. ll prek1[mang],prek2[mang];
  18. ll sum = 0;
  19. ll tinh(ll c,ll x,ll y){
  20. ll cl = sum - a[c];
  21. ll ans = 0;
  22. if(cl < y) ans += y - cl;
  23. if(a[c] < x) ans += x - a[c];
  24. return ans;
  25. }
  26. int main(){
  27. cin.tie(0) -> sync_with_stdio(0);
  28. // freopen("LEGO.INP","r",stdin);
  29. // freopen("LEGO.OUT","w",stdout);
  30. cin >> n;
  31. a.resize(n + 1);
  32. FOR(i,1,n) cin >> a[i],sum += a[i];
  33. sort(a.begin() + 1,a.end());
  34. a.push_back(MAX);
  35. cin >> m;
  36. FOR(i,1,m){
  37. ll x,y;
  38. cin >> x >> y;
  39. ll c = lower_bound(a.begin() + 1,a.end(),x) - a.begin();
  40. ll c1;
  41. if(c > 1) c1 = c - 1;
  42. else c1 = c;
  43. cout << min(tinh(c,x,y),tinh(c1,x,y)) << el;
  44. }
  45. }
  46.  
Success #stdin #stdout 0s 5300KB
stdin
Standard input is empty
stdout
Standard output is empty