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
#include <cstdio>
#include <vector>
#include <algorithm>

struct cmp {
    bool operator()(const unsigned left, const unsigned right) const
    {
        return (right < left);
    }
};

//void print(std::vector<unsigned>& v)
/* {
    printf("Vector; size(%lu)\n[", v.size());
    for(auto& el: v) {
         printf("%u, ", el);
    }
    printf("]\n");
}
*/

void print_tree(std::vector<unsigned>& tab) {
    // Tree must have sorted nodes from highest to lowest degree
    printf("%lu\n", tab.size());
    size_t left = 0;
    size_t right = tab.size() - 1;
    while(left < right) {
        int left_degree = tab[left];
       // int right_degree = tab[right];
        while(left_degree > 1) {
            printf("%lu %lu\n", left + 1, right + 1);
            right--;
            left_degree--;
        }
        printf("%lu %lu\n", left + 1, left + 2);
        left++;
        tab[left]--;
    }
}

int main()
{
    unsigned n;
    scanf("%u", &n);
    std::vector<unsigned> tab(n);
    unsigned current = 0;
    unsigned long total_sum = 0;
    for(unsigned i = 0; i < n; i++) {
        scanf("%u", &current);
        tab[i] = current;
        total_sum += current;
    }
    std::sort(tab.begin(), tab.end());
    std::vector<unsigned> full_copy(tab);
    unsigned count = n;
    while((count > 2) && (total_sum > (2 * (count - 1)))) {
        total_sum -= tab.back();
        tab.pop_back();
        count--;
    }
    //print(tab);
    std::reverse(tab.begin(), tab.end());
    if (total_sum < (2 * (count - 1))) {
        tab.back() += 2*(count -1) - total_sum;
        printf("1\n");
    } else if (count == 2) {
        unsigned change = 0;
        for(auto& el: tab) {
            if (el != 1) {
                change += 1;
            }
        }
        //print(tab);
        printf("%u\n2\n1 2", change);
        return 0;
    } else {
        printf("0\n");
    }
    print_tree(tab);
    return 0;
}