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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <bits/stdc++.h>
#include <math.h>
#include <chrono>
using namespace std;
#pragma GCC optimize("-O3")
#define endl "\n"
#define mp make_pair
#define st first
#define nd second
#define pii pair<int, int>
#define pb push_back
#define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10) //cin.tie(0); cout.tie(0);
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FWD(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fwd(i, a, b) for (int i = (a); i < (b); ++i)
#define all(c) (c).begin(), (c).end()
#define what(x) cerr << #x << " is " << x << endl;
#define int long long
//#define per tuple<long long, long long, short;

struct per
{
    long long przedstawiciel;
    long long ile;
    short val;
    void wypisz()
    {
        cerr << przedstawiciel << " " << ile << " " << val << endl;
    }
};

const int MAXN = 42;
const int MAXS = 5000000;

int n;
int pot2[MAXN];
int tab[MAXN];
int mnoznik[MAXN];
per tmp[MAXS];
int val1[1 << (MAXN / 2)];
int val2[1 << (MAXN / 2)];
int mask = 0;
vector<pair<char, char>> przedzialy;
vector<int> liczby;
vector<int> pref;
vector<per> V = {{0, 1, 0}};

per newper(per &stara, int gosc)
{
    per nowa = stara;
    nowa.val += (__builtin_popcountll(nowa.przedstawiciel >> gosc));
    nowa.przedstawiciel |= pot2[gosc];
    return nowa;
}

void update_mask(int k)
{
    mask |= pot2[k];
    przedzialy.clear();
    liczby.clear();
    pref.clear();
    int last = -1;
    rep(i, MAXN)
    {
        if (mask & pot2[i])
        {
            if (last == -1)

                last = i;
        }
        else if (last != -1)
        {
            przedzialy.push_back({last, i - 1});
            last = -1;
        }
    }
    for (auto P : przedzialy)
    {
        int a = 0;
        for (int i = P.st; i <= P.nd; i++)
            a += pot2[i];
        liczby.push_back(a);
    }
    pref.push_back(1);
    for (auto P : przedzialy)
    {
        pref.push_back(pref.back() * (P.nd - P.st + 2));
    }
    for (int j = 0; j < przedzialy.size(); j++)
    {
        auto P = przedzialy[j];
        for (int l = P.st; l <= P.nd; l++)
            mnoznik[l] = pref[j];
    }

    for (int S = 0; S < pot2[20]; S++)
    {
        val1[S] = 0;
        val2[S] = 0;
        for (int i = 0; i < 20; i++)
            if (S & pot2[i])
            {
                int a = S ^ pot2[i];
                val1[S] = val1[a] + mnoznik[i];
                val2[S] = val2[a] + mnoznik[i + 20];
                break;
            }
    }
}

inline int get_val(int a = pot2[n] - 1)
{
    int b = a >> 20;
    return val2[b] + val1[a - (b << 20)];
    /*
    int ans = 0;
    rep(i, n) if (a & pot2[i])
        ans += mnoznik[i];
    return ans;
    */
}

void add(per &A, per &B)
{
    if (A.val == B.val)
    {
        A.ile += B.ile;
        return;
    }
    if (A.val > B.val)
        A = B;
}
void debug()
{
    cerr << "wypisuje V, o rozmiarze: " << V.size() << endl;
    for (auto P : V)
    {
        cerr << "ma wartosc: " << get_val(P.przedstawiciel) << " ||  ";
        P.wypisz();
    }
    cerr << "wypisuje przedzialy: " << endl;
    for (auto P : przedzialy)
        cerr << P.st << " " << P.nd << endl;
}

void update(int k)
{
    update_mask(k);
    int l = get_val();
    cerr << "takie sa wartosci:" << k << " " << l << " " << mask << " " << endl;
    rep(i, l + 1) tmp[i].val = MAXN * MAXN;

    int s = V.size();
    what(s);
    for (auto &P : V)
    {
        int a = get_val(P.przedstawiciel);
        add(tmp[a], P);

        auto Q = newper(P, k);
        add(tmp[a + mnoznik[k]], Q);
    }

    //debug();

    V.resize(l + 1);
    rep(i, l + 1) V[i] = tmp[i];
}

void pre()
{
    pot2[0] = 1;
    for (int i = 1; i < MAXN; i++)
        pot2[i] = pot2[i - 1] * 2;
    cin >> n;
    rep(i, n)
    {
        cin >> tab[i];
        tab[i]--;
    }
    // rep(i, n) cerr << tab[i] << " ";
    //cerr << endl;
}

main()
{
    _upgrade;
    pre();
    V.reserve(MAXS);
    rep(i, n)
    {
        update(tab[i]);
        //   debug();
    }
    vector<pii> odp(n + 2);
    for (auto P : V)
        odp[__builtin_popcountll(P.przedstawiciel)] = {P.val, P.ile};

    for (int i = 1; i <= n; i++)
        cout << odp[i].st << " " << odp[i].nd << endl;
}