malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int par1[10008];   int par2[10008];   vector<pair<int, int>> result;   int find1(int r) {     if (par1[r] == r)       return r;     else       return par1[r] = find1(par1[r]);   }   int find2(int r) {     if (par2[r] == r)       return r;     else       return par2[r] = find2(par2[r]);   }   int main() {     int i, j, t, n, m1, m2, p, q, res = 0, uu, vv, uu2, vv2;     cin >> n >> m1 >> m2;     for (i = 1; i <= n; i++) {       par1[i] = i;       par2[i] = i;     }     for (i = 0; i < m1; i++) {       cin >> p >> q;       uu = find1(p);       vv = find1(q);       par1[uu] = vv;     }     for (i = 0; i < m2; i++) {       cin >> p >> q;       uu2 = find2(p);       vv2 = find2(q);       par2[uu2] = vv2;     }     for (i = 1; i <= n; i++) {       for (j = 1; j <= n; j++) {         uu = find1(i);         vv = find1(j);         uu2 = find2(i);         vv2 = find2(j);         if ((uu != vv) && (uu2 != vv2)) {           res++;           par1[uu] = vv;           par2[uu2] = vv2;           result.push_back({i, j});         }       }     }     cout << res << endl;     for (i = 0; i < result.size(); i++) {       cout << result[i].first <<     << result[i].second << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e17 + 10;   const int N = 1e4 + 10;   const long long mod = 998244353;   map<string, int> ml;   int c[N], vis[N], a[N], t, n, m, x, y, k, dp[N][105];   char s[N];   int ex, ey, cnt;   long long dist[N];   vector<pair<int, int> > v;   map<int, int> mp;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       mp[a[i]]++;     }     for (auto p : mp) v.push_back(p);     if (v.size() == 2) return cout << n << endl, 0;     dp[0][0] = 1;     for (auto p : v) {       int cost = p.first;       int k = p.second;       for (int i = N; i >= 0; i--)         for (int j = 1; j <= n; j++)           for (int t = 1; t <= k && t <= j && t * cost <= i; t++)             dp[i][j] += dp[i - t * cost][j - t];     }     int ans = 1;     for (auto p : v) {       int cost = p.first;       int k = p.second;       for (int i = 1; i <= k; i++) {         if (dp[i * cost][i] == 1) ans = max(ans, i);       }     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     vector<pair<int, int> > a;     vector<int> b;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       int t;       scanf( %d , &t);       if (t & 1) {         int spd = 0;         if (t != 5) scanf( %d , &spd);         a.push_back({t, spd});       } else {         b.push_back(t);       }     }     int res = 0;     int spd = 0;     vector<int> lim;     lim.push_back(1e9);     for (pair<int, int> p : a) {       if (p.first == 1) {         spd = p.second;         while (spd > lim.back()) {           lim.pop_back();           res++;         }       }       if (p.first == 3) {         if (spd <= p.second)           lim.push_back(p.second);         else           res++;       }       if (p.first == 5) lim.push_back(1e9);     }     vector<bool> allowed;     allowed.push_back(1);     for (int p : b) {       if (p == 2) {         while (!allowed.back()) {           allowed.pop_back();           res++;         }       }       if (p == 4) allowed.push_back(1);       if (p == 6) allowed.push_back(0);     }     printf( %d n , res);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long i, j, k, t, l, r, n, m, p, a, b, c, d, flag;     cin >> t;     while (t--) {       cin >> n;       for (i = 0; i < n; i++) {         cout << (i + 1) <<    ;       }       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 0;     char ch = getchar();     while (!isdigit(ch)) f |= ch ==  - , ch = getchar();     while (isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();     return f ? -x : x;   }   int main() {     int n = read(), ans = 0;     for (int i = 1; i <= n; ++i) ans += read() * i;     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, t, i, k, tam;   int main() {     while (scanf( %I64d , &n) > 0) {       t = 0;       tam = 0;       for (i = 0; i < n; i++) {         scanf( %I64d , &k);         if (k % 2 != 0) {           tam = k;         }         t += k;       }       if (t % 2 != 0) {         printf( First n );       } else {         if ((t - tam) % 2 != 0)           printf( First n );         else           printf( Second n );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, k, sum;     while (cin >> n >> k) {       for (int i = k; i >= 1; i--) {         sum = ((n * k) / (i)) + i;         if ((sum / k) * (sum % k) == n) {           break;         }       }       cout << sum << endl;     }     return 0;   }
#include <bits/stdc++.h>   #pragma warning(disable : 4996)   #pragma comment(linker,  /STACK:336777216 )   using namespace std;   int IT_MAX = 1 << 17;   int MOD = 1000000007;   const int INF = 0x3f3f3f3f;   const long long LL_INF = 0x3f3f3f3f3f3f3f3f;   const double PI = acos(-1);   const double ERR = 1e-10;   long long dp[1050][10050];   long long C[10050];   long long cost[10050];   int main() {     int N, W, B, X, i, j, k;     scanf( %d %d %d %d , &N, &W, &B, &X);     for (i = 1; i <= N; i++) scanf( %lld , &C[i]);     for (i = 1; i <= N; i++) scanf( %lld , &cost[i]);     for (i = 0; i <= N; i++)       for (j = 0; j <= 10000; j++) dp[i][j] = -LL_INF;     dp[0][0] = W;     for (i = 1; i <= N; i++) {       for (j = 0; j <= 10000; j++) {         long long v = dp[i - 1][j];         if (v < 0) continue;         long long mx = C[i];         if (cost[i]) mx = min(mx, v / cost[i]);         for (k = 0; k <= mx; k++)           dp[i][j + k] = max(dp[i][j + k], dp[i - 1][j] - cost[i] * k);       }       for (j = 0; j <= 10000; j++)         if (dp[i][j] >= 0) dp[i][j] = min(dp[i][j] + X, (long long)B * j + W);     }     int ans = 0;     for (i = 1; i <= 10000; i++)       if (dp[N][i] >= 0) ans = max(ans, i);     return !printf( %d n , ans);   }
#include <bits/stdc++.h>   using namespace std;   int row[1010];   int main() {     int n, m, k;     scanf( %d %d %d , &n, &m, &k);     memset(row, 0x3f3f3f3f, sizeof row);     long long ans = 0;     for (int i = (0); i < (n); ++i) {       int r, c;       scanf( %d %d , &r, &c);       row[r] = min(c, row[r]);     }     for (int i = 1; i <= m; ++i) ans += ((row[i] == 0x3f3f3f3f) ? 0 : row[i]);     cout << min(ans, (long long)k) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, sol[1000005];   struct ell {     int dr, cnt, poz;     bool operator<(const ell &A) const { return dr < A.dr; }   };   multiset<ell> Q;   struct el {     int x, y, flag, cnt, poz;     bool operator<(const el &A) const {       if (x == A.x) {         if (y == A.y) return flag > A.flag;         return y > A.y;       }       return x < A.x;     }   };   el a[1000005], b[1000005];   int main() {     int i, j, cn;     ell w;     cin.sync_with_stdio(0);     cin >> n;     cn = n;     for (i = 1; i <= n; ++i) {       cin >> a[i].x >> a[i].y;       a[i].poz = i;       b[i] = a[i];     }     cin >> m;     for (i = 1; i <= m; ++i) {       cin >> a[n + i].x >> a[n + i].y >> a[n + i].cnt;       a[n + i].flag = 1;       a[n + i].poz = i;       b[n + i] = a[n + i];     }     n += m;     sort(a + 1, a + n + 1);     for (i = 1; i <= n; ++i) {       if (a[i].flag) {         w.dr = a[i].y;         w.cnt = a[i].cnt;         w.poz = a[i].poz;         Q.insert(w);       } else {         ell w;         w.dr = a[i].y;         multiset<ell>::iterator it = Q.lower_bound(w);         if (it == Q.end()) {           printf( NO n );           return 0;         }         sol[a[i].poz] = it->poz;         --b[it->poz + cn].cnt;         if (!b[it->poz + cn].cnt) Q.erase(it);       }     }     cout <<  YES n ;     for (i = 1; i <= n - m; ++i) cout << sol[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int gcd(long long int a, long long int b) {     if (b > a) swap(a, b);     if (a % b == 0)       return b;     else       return gcd(b, a % b);   }   long long int a, b, k, k1, k2;   int main() {     cin >> a >> b;     k = (a * b) / gcd(a, b);     k1 = k / a;     k2 = k / b;     if (abs(k1 - k2) == 1)       cout <<  Equal ;     else if (k1 > k2)       cout <<  Dasha ;     else       cout <<  Masha ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   const int MAXH = 1000000;   const int MAXW = 1000000;   const int MAXN = 100000;   const int MOD = 1000000007;   typedef struct R {     int x1, y1, x2, y2;   } R;   int h, w, n;   int shead[MAXH], snxt[MAXN];   int thead[MAXH], tnxt[MAXN];   R r[MAXN];   int cur[MAXN], ncur;   int sval[4 * MAXW];   int slazy[4 * MAXW];   inline void inc(int &a, int b) {     if ((a += b) >= MOD) a -= MOD;   }   void spush(int x) {     if (sval[x] != -1) {       sval[2 * x + 1] = sval[2 * x + 2] = sval[x];       slazy[2 * x + 1] = slazy[2 * x + 2] = 0;       sval[x] = -1;     }     if (slazy[x] != 0) {       inc(slazy[2 * x + 1], slazy[x]), inc(slazy[2 * x + 2], slazy[x]);       slazy[x] = 0;     }   }   void sset(int x, int l, int h, int L, int H, int VAL) {     if (h < L || l > H) return;     if (L <= l && h <= H) {       sval[x] = VAL, slazy[x] = 0;       return;     }     spush(x);     int m = l + (h - l) / 2;     sset(2 * x + 1, l, m, L, H, VAL);     sset(2 * x + 2, m + 1, h, L, H, VAL);   }   void sinc(int x, int l, int h, int L, int H, int BY) {     if (h < L || l > H) return;     if (L <= l && h <= H) {       inc(slazy[x], BY);       return;     }     spush(x);     int m = l + (h - l) / 2;     sinc(2 * x + 1, l, m, L, H, BY);     sinc(2 * x + 2, m + 1, h, L, H, BY);   }   int sget(int x, int l, int h, int IDX) {     if (sval[x] != -1) return sval[x] + slazy[x];     spush(x);     int m = l + (h - l) / 2;     return IDX <= m ? sget(2 * x + 1, l, m, IDX) : sget(2 * x + 2, m + 1, h, IDX);   }   void run() {     scanf( %d%d%d , &h, &w, &n);     for (int x = (0); x < (h); ++x) shead[x] = thead[x] = -1;     for (int i = (0); i < (n); ++i) {       scanf( %d%d%d%d , &r[i].x1, &r[i].y1, &r[i].x2, &r[i].y2);       --r[i].x1, --r[i].y1, --r[i].x2, --r[i].y2;       snxt[i] = shead[r[i].x1], shead[r[i].x1] = i;       tnxt[i] = thead[r[i].x2], thead[r[i].x2] = i;     }     multiset<int> have;     have.insert(w);     sval[0] = slazy[0] = 0;     sset(0, 0, w - 1, 0, 0, 1);     for (int x = (0); x < (h); ++x) {       for (int z = shead[x]; z != -1; z = snxt[z])         sset(0, 0, w - 1, r[z].y1, r[z].y2, 0), have.insert(r[z].y1);       ncur = 0;       if (x == 0)         cur[ncur++] = 0;       else         for (int z = thead[x - 1]; z != -1; z = tnxt[z]) {           if (r[z].y1 != 0) cur[ncur++] = r[z].y1 - 1;           multiset<int>::iterator it = have.lower_bound(r[z].y1);           have.erase(it);         }       sort(cur, cur + ncur);       ncur = unique(cur, cur + ncur) - cur;       for (int i = ncur - 1; i >= 0; --i) {         int y = cur[i];         int val = sget(0, 0, w - 1, y);         if (val == 0) continue;         multiset<int>::iterator it = have.lower_bound(y);         int ny = *it - 1;         if (y + 1 <= ny) sinc(0, 0, w - 1, y + 1, ny, val);       }     }     int ret = sget(0, 0, w - 1, w - 1);     printf( %d n , ret);   }   int main() {     run();     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   template <typename T, typename TT>   inline ostream &operator<<(ostream &os, const pair<T, TT> &t) {     return os << t.first <<     << t.second;   }   template <typename T>   inline ostream &operator<<(ostream &os, const vector<T> &t) {     for (auto i : t) os << i <<    ;     return os;   }   template <typename T>   inline istream &operator>>(istream &is, vector<T> &v) {     for (T &t : v) is >> t;     return is;   }   template <typename T1, typename T2>   inline istream &operator>>(istream &is, pair<T1, T2> &t) {     is >> t.first >> t.second;     return is;   }   const long long mod = 1e9 + 7;   void solve() {     int n;     cin >> n;     vector<int> val(3 * n), vis(3 * n + 1);     cin >> val;     vector<int> loc(3 * n + 1);     vector<vector<long long> > v(n, vector<long long>(3));     for (int i = 0; i < n; i++) {       for (int j = 0; j < 3; j++) {         cin >> v[i][j];         loc[v[i][j]] = i;       }     }     int k, j = 0;     cin >> k;     vector<int> ans, up;     for (int i = 0; i < loc[k]; i++) {       for (auto x : v[i]) vis[x] = 1, up.push_back(x);     }     while (vis[val[j]]) j++;     ;     if (k != val[j]) {       for (int i = 1; i <= 3 * n; i++)         if (i != k) cout << i <<    ;       return;     }     for (auto x : v[loc[k]])       if (x != k) ans.push_back(x);     for (int i = loc[k] + 1; i < n; i++) {       for (auto x : v[i]) ans.push_back(x);     }     sort(ans.begin(), ans.begin() + 2);     sort(ans.begin() + 2, ans.end());     sort(up.begin(), up.end());     ;     j = 0;     for (auto x : ans) {       while (j < up.size() and up[j] < x) cout << up[j++] <<    ;       cout << x <<    ;     }     cout << vector<long long>(up.begin() + j, up.end()) <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     ;     int T = 1;     while (T--) {       solve();     }     return 0;   }
/*      Author: ChinmayKarnik      Time: 21:14:26      Date: 16-04-2021   */      #include<bits/stdc++.h>      using namespace std;      //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   //mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());      typedef enum {BLACK,WHITE,GRAY} color;      #define int long long    #define TESTCASES true   #define FASTIO true   const int maxN = 200009;   const int inf  = (int)1e18 + 1;   int N,K;   int a[maxN];      int p[64];         void solve()   {       int i,j,k;       vector<int> ans;          cin >> N >> K;        int one = 1;          if(N<64)       {          if((one<<(N-1))<K)          {              cout <<  -1 n ;              return;          }       }          while(N>=64)       {           ans.push_back(1);           N--;       }          while(N)       {           int curr = 1;           int sub = 0;           while((curr<=N) && p[N-curr]<K)           {               K-=p[N-curr];               curr++;           }          // curr--;           N-=curr;           ans.push_back(curr);           //K-=(curr-1)*(p[])       }          int p = 1;       vector<int> fin;          for(int x:ans)       {           for(i=p+x-1;i>=p;i--)           {               fin.push_back(i);           }           p=p+x;       }              for(int x:fin)       {           cout << x <<    ;       }       cout <<   n ;                 }         signed main()   {       if(FASTIO)       {           ios_base::sync_with_stdio(false);           cin.tie(NULL);       }       int T = 1;          if(TESTCASES)       {           cin >> T;       }       int i,j,k;              p[0]=1;       p[1]=1;       for(i=2;i<64;i++)       {           p[i]=0;           for(j=0;j<i;j++)           {               p[i]+=p[j];           }                     // cout << p[i] <<    ;       }          while(T--)       {           solve();       }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     vector<int> v;     int k, n, fl = 0;     cin >> n >> k;     v.resize(n);     for (int i = 0; i < n; ++i) {       cin >> v[i];       if (v[i] == k) fl = 1;     }     int ct = 0;     if (!fl) {       v.push_back(k);       ct++;       ++n;     }     sort(v.begin(), v.end());     int p, d = 1e9;     for (int i = 0; i < v.size(); ++i) {       if (k == v[i] && abs((n + 1) / 2 - (i + 1)) < d) {         p = i + 1;         d = abs((n + 1) / 2 - (i + 1));       }     }     while (p < (n + 1) / 2) {       ++ct;       ++p;       ++n;     }     while (p > (n + 1) / 2) {       ++ct;       ++n;     }     cout << ct;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long bananaz(long long x, long long y) {     return (x + 1) * (y + 1) * (x + y) / 2;   }   int main() {     ios_base::sync_with_stdio(false);     long long m, b, x = 0, y, max = -1;     cin >> m >> b;     y = b;     while (y >= 0) {       if (bananaz(x, y) > max) max = bananaz(x, y);       x++;       while (y > -1.0 * x / m + b) y--;     }     cout << max << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int goal, n, stations;     scanf( %i%i%i , &goal, &n, &stations);     vector<pair<int, int> > points;     points.push_back(make_pair(n, -1));     points.push_back(make_pair(goal, 2000000000));     for (auto e = 0; e < stations; e++) {       int a, b;       scanf( %i%i , &a, &b);       points.push_back(make_pair(a, b));       points.push_back(make_pair(a + n, -b - 1));     }     stable_sort(points.begin(), points.end());     multiset<int> sw;     auto prev = make_pair(0, 0);     sw.insert(0);     auto acc = 0LL;     for (auto e : points) {       if (sw.empty()) {         if (e.first != prev.first) {           acc = -1;           break;         }       } else {         acc += ((long long)e.first - prev.first) * (*sw.begin());       }       if (e.first == goal) {         break;       }       if (e.second < 0) {         sw.erase(sw.find(-e.second - 1));       } else {         sw.insert(e.second);       }       prev = e;     }     printf( %I64d n , acc);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = (int)555;   const int mod = (int)1e9 + 7;   const int P = (int)1e6 + 7;   const double pi = acos(-1.0);   int a[maxn][maxn], b[maxn][maxn], n;   vector<vector<int> > base{{1, 2, 4}, {5, 3, 8}, {9, 6, 7}};   void rec(int n) {     if (n == 3) {       for (int i = (0); i <= (n - 1); ++i)         for (int j = (0); j <= (n - 1); ++j) a[i][j] = base[i][j];       return;     }     rec(n - 1);     if (!(n & 1)) {       int cur = 1;       for (int i = (n - 1); i >= (1); --i) b[i][n - 1] = cur++;       for (int i = (n - 1); i >= (0); --i) b[0][i] = cur++;       for (int i = (1); i <= (n - 1); ++i)         for (int j = (0); j <= (n - 2); ++j) b[i][j] = cur + a[i - 1][j] - 1;     } else {       int cur = 1;       for (int i = (0); i <= (n - 1); ++i) b[0][i] = cur++;       for (int i = (1); i <= (n - 1); ++i) b[i][n - 1] = cur++;       for (int i = (1); i <= (n - 1); ++i)         for (int j = (0); j <= (n - 2); ++j) b[i][j] = cur + a[i - 1][j] - 1;     }     for (int i = (0); i <= (n - 1); ++i)       for (int j = (0); j <= (n - 1); ++j) a[i][j] = b[i][j];   }   void solve() {     scanf( %d , &n);     if (n <= 2) {       puts( -1 );       return;     }     rec(n);     for (int i = (0); i <= (n - 1); ++i) {       for (int j = (0); j <= (n - 1); ++j) printf( %d  , a[i][j]);       puts(  );     }   }   int main() {     int t = 1;     while (t--) solve();   }
#include <bits/stdc++.h>   int gcd(int a, int b) {     if (a == 0)       return b;     else       return (b % a, a);   }   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     int a[n];     long long sum = 0;     for (int i = 0; i < n; i++) {       cin >> a[i];       sum += a[i];     }     sort(a, a + n);     long long tot = 0;     for (int i = n - 1; i >= 0; i--) {       tot += a[i];       if (tot > sum - tot) {         cout << n - i << endl;         return 0;       }     }     return 0;   }
//#pragma GCC optimize( Ofast )   #include <bits/stdc++.h>   using namespace std;   typedef long long int ll;   typedef unsigned long long ull;       mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());   ll myRand(ll B) {    return (ull)rng() % B;   }      int main(){       cin.tie(nullptr);       ios::sync_with_stdio(false);       int n,k; cin >> n >> k;       vector<int> l(n);       for(int i=0;i<n;i++){           cin >> l[i];       }       sort(l.rbegin(), l.rend());       const int N=5e5;       vector<int> a(N);       a[0]=1,a[1]=-1;       int ans=1e9;       int sum=0;       for(int i=0,j=0;i<N-1;i++){           sum+=a[i];           a[i+1]+=a[i];           if(sum+a[i+1]>=k){               ans=i+1; break;           }           while(a[i]>0 and j<n){               int u=(l[j]-1)/2;               int v=l[j]-1-u;               a[i+2]++;               a[i+2+u]--;               a[i+2]++;               a[i+2+v]--;               a[i]--;               sum--;               j++;           }       }       if(ans==1e9){           ans=-1;       }       cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   const int M = 1e5 + 10;   const int INF = 2147483647;   const int MOD = 1e9 + 7;   int T, n, m;   int sum;   int a[N];   pair<int, int> b[N];   long long gcd(long long x, long long y) { return y == 0 ? x : gcd(y, x % y); }   long long qpow(long long base, long long x) {     long long res = 1;     while (x) {       if (x & 1) res *= base;       base *= base;       base %= MOD;       res %= MOD;       x >>= 1;     }     return res;   }   long long qmul(long long a, long long b, long long mod) {     long long res = 0;     while (b) {       if (b & 1) res = (res + a) % mod;       a = (a + a) % mod;       b >>= 1;     }     return res;   }   void exgcd(long long a, long long b, long long &x, long long &y) {     if (b == 0) {       x = 1;       y = 0;       return;     }     exgcd(b, a % b, y, x);     y -= (a / b) * x;   }   long long getinv(long long x, long long mod) {     long long x_0, y_0;     exgcd(x, mod, x_0, y_0);     return x_0;   }   bool check(int x) {     if (sum > x) return false;     vector<int> day(n + 1, 0);     for (int i = 1; i <= m; i++) {       int d = b[i].first, item = b[i].second;       if (d <= x) {         day[item] = max(day[item], d);       }     }     vector<vector<int>> tmp(200005);     for (int i = 1; i <= n; i++) {       if (day[i]) {         tmp[day[i]].emplace_back(i);       }     }     vector<int> use(n + 1, 0);     for (int i = 1; i <= n; i++) use[i] = a[i];     int cur = 0;     for (int i = 1; i <= x; i++) {       ++cur;       if (i > 200000) continue;       for (auto &t : tmp[i]) {         if (cur >= a[t]) {           cur -= a[t];           use[t] = 0;         } else {           use[t] -= cur;           cur = 0;           break;         }       }     }     int ss = 0;     for (int i = 1; i <= n; i++) ss += use[i];     return cur >= ss * 2;   }   void solve() {     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> a[i], sum += a[i];     for (int i = 1; i <= m; i++) cin >> b[i].first >> b[i].second;     int l = 1, r = 1e9;     while (l < r) {       int mid = (l + r) >> 1;       if (check(mid))         r = mid;       else         l = mid + 1;     }     cout << l << endl;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(nullptr);     T = 1;     while (T--) solve();     return 0;   }