1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>
#include <vector>
#include <queue>
#include <set>

using namespace std;

/*
Autor: Helena Borak-Wejster
*/

bool is_ok(int k, int sum) {
    return (k-1)*2==sum;
}

void create_tree_with_modification(vector<int> d) {
    int n = d.size();
    priority_queue<pair<int,int>> Q;
    for(int i=0;i<n;i++){
        Q.push(make_pair(d[i],i));
    }

    set<int> left_dg;
    set<int> used;
    set<int> touched;
    vector<pair<int,int>> out_pairs;

    while(used.size()<n && !Q.empty()){
        auto m = Q.top();
        Q.pop();
        used.insert(m.second);
        touched.insert(m.second);

        int dg = m.first;
        queue<pair<int,int>> tQ;
        while(dg>0){
            if(Q.empty()){
                left_dg.insert(m.second);
                break;
            }
            auto j = Q.top();
            Q.pop();
            int id1=m.second+1;
            int id2=j.second+1;
            out_pairs.push_back(make_pair(id1,id2));
            //cout << m.second+1 <<" "<< j.second+1 <<endl;
            touched.insert(j.second);
            dg--;
            j.first--;
            tQ.push(j);
        }
        while(!tQ.empty()){
            auto j = tQ.front();
            tQ.pop();
            if(j.first>0) Q.push(j);
        }
    }
    //cout << n-used.size()-(n-touched.size()) << endl;
    cout << left_dg.size() << endl;
    cout << touched.size() << endl;
    for(auto it=out_pairs.begin();it!=out_pairs.end();it++) cout << (*it).first << " " << (*it).second << endl;
}

void create_tree(vector<int> d) {
    int n = d.size();
    priority_queue<pair<int,int>> Q;
    for(int i=0;i<n;i++){
        Q.push(make_pair(d[i],i));
    }

    while(!Q.empty()){
        auto m = Q.top();
        Q.pop();

        int dg = m.first;
        queue<pair<int,int>> tQ;
        while(dg>0){
            auto j = Q.top();
            Q.pop();
            cout << m.second+1 <<" "<< j.second+1 <<endl;
            dg--;
            j.first--;
            tQ.push(j);
        }
        while(!tQ.empty()){
            auto j = tQ.front();
            tQ.pop();
            if(j.first>0) Q.push(j);
        }
    }
}

void add_one_binary(int* a, int n){
    if(a[0]==0) {
        a[0]=1;
    }
    else {
        int i=0;
        while(a[i]!=0 && i<n) {
            a[i++]=0;
        }
        a[i]=1;
    }
}

int how_many(int* a, int n){
    int c = 0;
    for(int i=0;i<n;i++){
        if(a[i]!=0) c++;
    }
    return c;
}

void find_sub(vector<int> v) {
    int n = v.size();
    int a[n];
    for(int i=0;i<n;i++) {
        a[i]=0;
    }
    while(how_many(a, n)<n){
        add_one_binary(a, n);
        //for(int i=0;i<n;i++) cout << a[i] << " ";
        //cout << endl;
        if(how_many(a, n)<2) continue;
        int sum = 0;
        for(int i=0;i<n;i++){
            sum+=(a[i]==1?v[i]:0);
        }
        int hm = how_many(a,n);
        int cel = (how_many(a,n)-1)*2;
        if(sum==cel){
                cout << 0 << endl;
                cout << hm << endl;
                vector<int> p;
                for(int i=0;i<n;i++) if(a[i]) p.push_back(v[i]);
                create_tree(p);
                return;
        }
    }
    create_tree_with_modification(v);
}



int main()
{
    int k;
    cin >> k;
    vector<int> d;
    int sum=0;
    for(int i=0;i<k;i++) {
        int tmp;
        cin >> tmp;
        d.push_back(tmp);
        sum+=tmp;
    }

    if(is_ok(k, sum)) {
        cout << 0 << endl;
        cout << k << endl;
        create_tree(d);
    }
    else {
        find_sub(d);
    }

    return 0;
}