#include<iostream>
#include<vector>
#include<random>
using namespace std;
const int MAX_N=55;
const int MAX=2202;
mt19937 rng(903532);
bool stat[MAX+5][MAX+5];
int a[MAX_N];
int n,k;
vector<pair<int,int>>ans;
void rem(int target_row)
{
    for(int row=target_row;row<=MAX;row++)
    {
        for(int col=1;col<=n;col++)
        {
            stat[row][col]=stat[row+1][col];
        }
    }
}
bool full(int row)
{
    for(int col=1;col<=n;col++)
    {
        if(stat[row][col]==0)return 0;
    }
    return 1;
}
void fix()
{
    for(int row=1;row<=MAX;row++)
    {
        while(full(row))rem(row);
    }
    for(int col=1;col<=n;col++)
    {
        a[col]=0;
        while(stat[a[col]+1][col])a[col]++;
    }
}
void puthorizontal(int x)
{
    int y=0;
    for(int col=x;col<=x+k-1;col++)
    {
        y=max(y,a[col]);
    }
    for(int col=x;col<=x+k-1;col++)
    {
        stat[y+1][col]=1;
    }
    fix();
    ans.push_back({2,x});
}
void putvertical(int x)
{
    int y=a[x]+1;
    for(int row=y;row<=y+k-1;row++)
    {
        stat[row][x]=1;
    }
    fix();
    ans.push_back({1,x});
}
int firstbad()
{
    for(int i=2;i<=n;i++)
    {
        if(a[i]<a[i-1])return i;
    }
    return n+1;
}
int sum()
{
    int res=0;
    for(int i=1;i<=n;i++)res+=a[i];
    return res;
}
int firstbl(int row)
{
    for(int col=1;col<=n;col++)
    {
        if(stat[row][col])return col;
    }
    return n+1;
}
void check()
{
    return;
    for(int i=1;i<=n;i++)
    {
        cout<<a[i]<<" ";
    }cout<<"\n";
}
void print()
{
    cout<<ans.size()<<"\n";
    for(auto [type,where]:ans)
    {
        cout<<type<<" "<<where<<"\n";
    }
}
void balance()
{
    while(firstbad()<=n)
    {
        int i=firstbad();
        while(a[i]<a[i-1])
        {
            putvertical(i);
        }
    }
}
void solve()
{
    if(n%k==0 && sum()%k!=0)
    {
        //cout<<-1<<"\n";
       //return;
    }
    balance();check();
    int row=1;
    while(1)
    {
        int col=firstbl(row);
        if(col==n+1)break;
        if(col-1<k)
        {
            row++;continue;
        }
        for(int i=col-1;i>=k;i-=k)
        {
            puthorizontal(i-k+1);
        }
    }
    check();
    for(int col=1;col<k;col++)
    {
        check();
        int times=min(max(0,(MAX-a[col]))/k,190-5*(col-1));
        while(times>0)
        {
            putvertical(col);
            times--;
        }
    }
    check();
    if(n%k==0)
    {
        int ma=0;
        for(int col=1;col<k;col++)
        {
            ma=max(ma,a[col]);
            if(a[col]%k!=0)
            {
                cout<<-1<<"\n";
                return;
            }
        }
        for(int col=1;col<n;col++)
        {
            while(a[col]<ma){putvertical(col);check();}
        }
        int times=ma/k;
        while(times>0)
        {
            putvertical(n);
            times--;
        }
    }
    else
    {
        int ma=0;
        for(int col=1;col<=n%k;col++)
        {
            ma=max(ma,a[col]);
            if(col>1 && a[col]%k!=a[col-1]%k)
            {
                cout<<-1<<"\n";
                return;
            }
        }
        for(int col=n%k+1;col<=k;col++)
        {
            ma=max(ma,a[col]);
            if(a[col]%k!=0)
            {
                cout<<-1<<"\n";
                return;
            }
        }
        for(int col=1;col<=n%k;col++)
        {
            int cur=a[col];
            while(cur<ma)
            {
                putvertical(col);
                cur+=k;
            }
        }
        check();
        ma-=ma%k;
        for(int col=n%k+1;col<=n;col++)
        {
            int cur=a[col];
            if(col==n)
            {
                for(int i=1;i<=n%k;i++)
                {
                    if(a[i]!=a[1] or a[i]!=ma+a[i]%k)while(1);
                }
                for(int i=n%k+1;i<n;i++)
                {
                    if(a[i]!=a[n%k+1] or a[i]!=ma+k)while(1);
                }
                if(a[n%k+1]<a[1])while(1);
                if(a[col-1]%k!=0)while(1);
                if(a[n]!=0)while(1);
                int times=(ma)/k+1;
                while(times--)
                {
                    putvertical(n);
                }
            }
            else
            {
                while(cur<ma)
                {
                    putvertical(col);
                    cur+=k;
                }
                putvertical(col);
            }
            //if(col<n && a[col]!=a[n%k+1])while(1);
        }
        check();
        int rem=k-a[n%k+1];
        while(rem>0)
        {
            for(int col=n%k+1;col<=n;col+=k)
            {
                puthorizontal(col);
            }
            rem--;
        }
        check();
        for(int col=1;col<=n%k;col++)
        {
            putvertical(col);
        }
        ///
        int cnt=0;
        ma=0;
        for(int col=1;col<=n;col++)
        {
            if(a[col]!=0)
            {
                ma=col;
                cnt++;
            }
        }
        if(cnt!=0)
        {
        }
        check();
    }
    print();
}
int main ()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        for(int row=1;row<=a[i];row++)stat[row][i]=1;
    }
    solve();
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |