Language: C++

            int d = (a[i - 1] - a[i]) / a[j];
            while (d * a[j] + a[i] < a[i - 1]) ++d;
            if (d < mn) {
                mn = d;
                best = j;
            } else if (d == mn) {
                if (a[j] < a[best]) {
                    best = j;
                }
            }
        }
        if (mn == 2e9) return a;
        for (int j = 0; j < mn; ++j) {
            res.push_back({best + 1, i + 1});
        }
        a[i] += a[best] * mn;
    }
    return a;
}

bool good(vector<int> a) {
    auto b = a;
    sort(b.begin(), b.end());
    return a == b;
}

vector<pair<int, int>> dec(vector<int> a, int x) {
    int mn = 2e9;
    int val;
    int best;
    for (int i = 0; i < a.size(); ++i) {
        if (a[i] >= 0) continue;
        int dd = x / -a[i];
        while (abs(dd * a[i]) < x) {
            ++dd;
        }
        if (mn > dd) {
            mn = dd;
            val = dd * a[i];
            best = i;
        } else if (mn == dd) {
            if (val < dd * a[i]) {
                val = dd * a[i];
                best = i;
            }
        }
    }
    if (mn == 2e9) return {};
    vector<pair<int, int>> kek;
    for (int i = 0; i < mn; ++i) {
        kek.push_back({best + 1, 1});
    }
    return kek;
}

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    int l = -1, r = 10;
    vector<pair<int, int>> qwe;
    auto ans = qwe;
    while (r - l > 1) {
        int m = (l + r) / 2;
        auto w = dec(a, m);
        auto b = a;
        if (!w.empty()) b[0] += b[w[0].first - 1]  * w.size();
        auto c = f(b);
        if (good(c)) {
            r = m;
            ans = res;
        } else {
            l = m;
        }
    }
    qwe = dec(a, r);
    cout << res.size() + qwe.size() << '\n';
    for (auto w : ans) cout << w.first << " " << w.second << '\n';
    for (auto w : qwe) cout << w.first << " " << w.second << '\n';
}




Compiler output:
a.cc:1:22: error: 'a' was not declared in this scope
    1 |             int d = (a[i - 1] - a[i]) / a[j];
      |                      ^
a.cc:1:24: error: 'i' was not declared in this scope
    1 |             int d = (a[i - 1] - a[i]) / a[j];
      |                        ^
a.cc:1:33: error: 'a' was not declared in this scope
    1 |             int d = (a[i - 1] - a[i]) / a[j];
      |                                 ^
a.cc:1:35: error: 'i' was not declared in this scope
    1 |             int d = (a[i - 1] - a[i]) / a[j];
      |                                   ^
a.cc:1:41: error: 'a' was not declared in this scope
    1 |             int d = (a[i - 1] - a[i]) / a[j];
      |                                         ^
a.cc:1:43: error: 'j' was not declared in this scope
    1 |             int d = (a[i - 1] - a[i]) / a[j];
      |                                           ^
a.cc:2:13: error: expected unqualified-id before 'while'
    2 |             while (d * a[j] + a[i] < a[i - 1]) ++d;
      |             ^~~~~
a.cc:3:13: error: expected unqualified-id before 'if'
    3 |             if (d < mn) {
      |             ^~
a.cc:6:15: error: expected unqualified-id before 'else'
    6 |             } else if (d == mn) {
      |               ^~~~
a.cc:11:9: error: expected declaration before '}' token
   11 |         }
      |         ^
a.cc:12:9: error: expected unqualified-id before 'if'
   12 |         if (mn == 2e9) return a;
      |         ^~
a.cc:13:9: error: expected unqualified-id before 'for'
   13 |         for (int j = 0; j < mn; ++j) {
      |         ^~~
a.cc:13:25: error: 'j' does not name a type
   13 |         for (int j = 0; j < mn; ++j) {
      |                         ^
a.cc:13:33: error: expected unqualified-id before '++' token
   13 |         for (int j = 0; j < mn; ++j) {
      |                                 ^~
a.cc:16:9: error: 'a' does not name a type
   16 |         a[i] += a[best] * mn;
      |         ^
a.cc:17:5: error: expected declaration before '}' token
   17 |     }
      |     ^
a.cc:18:5: error: expected unqualified-id before 'return'
   18 |     return a;
      |     ^~~~~~
a.cc:19:1: error: expected declaration before '}' token
   19 | }
      | ^
a.cc:21:11: error: 'vector' was not declared in this scope
   21 | bool good(vector<int> a) {
      |           ^~~~~~
a.cc:21:18: error: expected primary-expression before 'int'
   21 | bool good(vector<int> a) {
      |                  ^~~
a.cc:27:1: error: 'vector' does not name a type
   27 | vector<pair<int, int>> dec(vector<int> a, int x) {
      | ^~~~~~
a.cc: In function 'int main()':
a.cc:58:5: error: 'cin' was not declared in this scope
   58 |     cin >> n;
      |     ^~~
a.cc:59:5: error: 'vector' was not declared in this scope
   59 |     vector<int> a(n);
      |     ^~~~~~
a.cc:59:12: error: expected primary-expression before 'int'
   59 |     vector<int> a(n);
      |            ^~~
a.cc:61:16: error: 'a' was not declared in this scope
   61 |         cin >> a[i];
      |                ^
a.cc:64:12: error: 'pair' was not declared in this scope
   64 |     vector<pair<int, int>> qwe;
      |            ^~~~
a.cc:64:17: error: expected primary-expression before 'int'
   64 |     vector<pair<int, int>> qwe;
      |                 ^~~
a.cc:65:16: error: 'qwe' was not declared in this scope
   65 |     auto ans = qwe;
      |                ^~~
a.cc:68:22: error: 'a' was not declared in this scope
   68 |         auto w = dec(a, m);
      |                      ^
a.cc:68:18: error: 'dec' was not declared in this scope
   68 |         auto w = dec(a, m);
      |                  ^~~
a.cc:71:18: error: 'f' was not declared in this scope
   71 |         auto c = f(b);
      |                  ^
a.cc:72:17: error: 'good' cannot be used as a function
   72 |         if (good(c)) {
      |             ~~~~^~~
a.cc:74:19: error: 'res' was not declared in this scope
   74 |             ans = res;
      |                   ^~~
a.cc:79:15: error: 'a' was not declared in this scope
   79 |     qwe = dec(a, r);
      |               ^
a.cc:79:11: error: 'dec' was not declared in this scope
   79 |     qwe = dec(a, r);
      |           ^~~
a.cc:80:5: error: 'cout' was not declared in this scope
   80 |     cout << res.size() + qwe.size() << '\n';
      |     ^~~~
a.cc:80:13: error: 'res' was not declared in this scope
   80 |     cout << res.size() + qwe.size() << '\n';
      |             ^~~