func_code_string: #include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m;     cin >> n >> m;     long long k[n];     pair<long long, long long> dt[m];     for (int i = 0; i < n; i++) cin >> k[i];     for (int i = 0; i < m; i++) cin >> dt[i].first >> dt[i].second;     long long total = 0;     for (int i = 0; i < n; i++) total += k[i];     long long start = 1;     long long end = 2 * total;     long long ans = 2 * total;     while (start <= end) {       long long check = start + (end - start) / 2;       pair<long long, long long> last[n];       for (int i = 0; i < n; i++) {         last[i].first = LONG_MAX;         last[i].second = i;       }       for (int i = 0; i < m; i++) {         if (dt[i].first <= check) {           if (last[dt[i].second - 1].first == LONG_MAX)             last[dt[i].second - 1].first = dt[i].first;           else             last[dt[i].second - 1].first =                 max(dt[i].first, last[dt[i].second - 1].first);         }       }       sort(last, last + n);       int point = 0;       long long money = 0;       long long count = 0;       for (int i = 1; i <= check; i++) {         money++;         while (last[point].first == i) {           long long deduce = min(money, k[last[point].second]);           count += deduce;           money -= deduce;           point++;         }       }       money -= 2 * (total - count);       if (money < 0) {         start = check + 1;       } else {         ans = check;         end = check - 1;       }     }     cout << ans << endl;     return 0;   }   