stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



#define pb push_back

#define mem(a) memset(a,0,sizeof(a))



typedef long long ll;

typedef pair<int,int> pii;



const int mn=55;



int n;

ll m,D;

int a[mn];

vector<int> g[mn];

struct item{

	ll v,w,num;//value weight

	bool operator < (const item &e) const{

		return v*e.w>e.v*w;

	}

}p[mn];

void dfs(int v,int x){

	p[x].w+=a[v];

	p[x].v++;

	for(int &i:g[v]){

		dfs(i,x);

	}

}



ll d[mn*mn*mn];



inline ll greedy(ll m){

	ll ans=0;

	for (int i = 1; i <= n; i++) {

		ll u = min(m / p[i].w,p[i].num);

		m -= u * p[i].w, ans += p[i].v * u;

	}

	return ans;

}



int main() {

	ios::sync_with_stdio(0);

	cin >> n >> m >> D;

	cin >> a[1];

	for (int i = 2; i <= n; i++) {

		int u;

		cin >> a[i] >> u;

		g[u].pb(i);

		p[i].num = max(D - 50,0ll);

	}

	p[1].num = 1e9;

	for (int i = 1; i <= n; i++) dfs(i, i);

	sort(p + 1, p + n + 1);

	int maxpos = 0;

	fill_n(d, mn*mn*mn, 1ll << 58);

	d[0] = 0;

	for (int i = 1; i <= n; i++) {

		for (int k = 1; k <= min(50ll, D); k++) {

			maxpos += p[i].v;

			for (int j = maxpos; j >= p[i].v; j--) {

				d[j] = min(d[j], d[j - p[i].v] + p[i].w);

			}

		}

	}

	ll ans=0;

	for (int i = 0; i <= maxpos; i++) {

		if (d[i] <= m) ans=max(ans,greedy(m-d[i])+i);

	}

	cout << ans;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.4201541708526095x):
#include<bits/stdc++.h>

using namespace std;

const int N=55;

int n,x,d,fa[N],c[N*N*N];

struct node

{

	int v,id;

	long long w;

	bool operator<(const node k)const

	{

		return v*k.w>w*k.v;

	}

}a[N];

long long dp[N*N*N],ans;

int main()

{

	ios::sync_with_stdio();

	cin>>n>>x>>d;

	for(int i=1;i<=n;i++)

	{

		cin>>a[i].w;

		if(i>1)

			cin>>fa[i];

		a[i].id=i;

		a[i].v=1;

	}

	for(int i=n;i>=1;i--)

	{

		a[fa[i]].w+=a[i].w;

		a[fa[i]].v+=a[i].v;

	}

	sort(a+1,a+n+1);

	int m=min(n,d);

	for(int j=1;j<=n*n*n;j++)

		dp[j]=1e18;

	for(int i=1;i<=n;i++)

	{

		for(int j=0;j<=n*n*n;j++)

			c[j]=0;

		for(int j=0;j<=n*n*n;j++)

		{

			if(c[j]==m)

				continue;

			if(dp[j+a[i].v]>dp[j]+a[i].w)

			{

				dp[j+a[i].v]=dp[j]+a[i].w;

				c[j+a[i].v]=c[j]+1;

			}

			else if(dp[j+a[i].v]==dp[j]+a[i].w&&c[j+a[i].v]>c[j]+1)

				c[j+a[i].v]=c[j]+1;

		}

	}

	for(int i=0;i<=n*n*n;i++)

	{

		if(dp[i]>x)

			continue;

		long long w=dp[i],v=i;

		for(int j=1;j<=n;j++)

		{

			if(a[j].id==1)

			{

				long long zh=(x-w)/a[j].w;

				w+=zh*a[j].w,v+=zh*a[j].v;

				continue;

			}

			long long zh=min((long long)d-m,(x-w)/a[j].w);

			w+=zh*a[j].w,v+=zh*a[j].v;

		}

		ans=max(ans,v);

	}

	cout<<ans<<endl;

	return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<cstring>

#define retrun return

#define cpnst const

int N,X,D;

typedef long long ll;

ll V[111111],W[111111],f[111111];

ll dp[133333];

cpnst int lim=131072;

void ins(ll v,ll w)

{

	register int i;

	for(i=lim;i>=w;i--)

		if(dp[i]>dp[i-w]+v)dp[i]=dp[i-w]+v;

}

struct pii

{ll v,w,s;}P[111];

bool operator<(cpnst pii x,cpnst pii y)

{return x.w*y.v>x.v*y.w;}

ll greedy(ll VAG)

{

	register int i;

	ll ret=0;

	for(i=1;i<=N;i++)

	{

		ll K=VAG/P[i].v;

		if(K>P[i].s)K=P[i].s;

		ret+=P[i].w*K,VAG-=P[i].v*K;

	}return ret;

}

void trial(ll v,ll w,int d)

{

	register int i;

	for(i=1;i<=d;d-=i,i<<=1)ins(v*i,w*i);

	if(d)ins(v*d,w*d);

}

#include<algorithm>

int main()

{

	register int i;

	scanf("%d%d%d",&N,&X,&D);

	int DL=D;

	if(N<DL)DL=N;

	scanf("%lld",V+1),W[1]=1;

	for(i=2;i<=N;i++)

		scanf("%lld%lld",V+i,f+i),W[i]=1;

	for(i=N;i>1;i--)

		V[f[i]]+=V[i],W[f[i]]+=W[i];

	for(i=1;i<=N;i++)

		P[i]=(pii){V[i],W[i],D-DL+X*(i==1)};

	std::sort(P+1,P+N+1);

	for(i=1;i<=lim;i++)dp[i]=2e9;

	for(i=1;i<=N;i++)trial(V[i],W[i],DL);

	ll ans=0;

	for(i=0;i<=lim;i++)

	{

		if(dp[i]>X)continue;

		ll T=greedy(X-dp[i])+i;

		if(T>ans)ans=T;

	}printf("%lld\n",ans);

}

/*

Please don't let me down.

*/

// Faster C++ solution for the same problem (measured speedup: 3.1419272039470263x):
#include<bits/stdc++.h>

using namespace std;

const int N=55;

int n,x,d,fa[N],c[N*N*N];

struct node

{

	int v,id;

	long long w;

	bool operator<(const node k)const

	{

		return v*k.w>w*k.v;

	}

}a[N];

long long dp[N*N*N],ans;

int main()

{

	ios::sync_with_stdio();

	cin>>n>>x>>d;

	for(int i=1;i<=n;i++)

	{

		cin>>a[i].w;

		if(i>1)

			cin>>fa[i];

		a[i].id=i;

		a[i].v=1;

	}

	for(int i=n;i>=1;i--)

	{

		a[fa[i]].w+=a[i].w;

		a[fa[i]].v+=a[i].v;

	}

	sort(a+1,a+n+1);

	int m=min(n,d);

	for(int j=1;j<=n*n*n;j++)

		dp[j]=1e18;

	for(int i=1;i<=n;i++)

	{

		for(int j=0;j<=n*n*n;j++)

			c[j]=0;

		for(int j=0;j<=n*n*n;j++)

		{

			if(c[j]==m)

				continue;

			if(dp[j+a[i].v]>dp[j]+a[i].w)

			{

				dp[j+a[i].v]=dp[j]+a[i].w;

				c[j+a[i].v]=c[j]+1;

			}

			else if(dp[j+a[i].v]==dp[j]+a[i].w&&c[j+a[i].v]>c[j]+1)

				c[j+a[i].v]=c[j]+1;

		}

	}

	for(int i=0;i<=n*n*n;i++)

	{

		if(dp[i]>x)

			continue;

		long long w=dp[i],v=i;

		for(int j=1;j<=n;j++)

		{

			if(a[j].id==1)

			{

				long long zh=(x-w)/a[j].w;

				w+=zh*a[j].w,v+=zh*a[j].v;

				continue;

			}

			long long zh=min((long long)d-m,(x-w)/a[j].w);

			w+=zh*a[j].w,v+=zh*a[j].v;

		}

		ans=max(ans,v);

	}

	cout<<ans<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define ll long long

#define int ll

using namespace std;

ll c[55],f[250050],sz[55],fa[55];

struct node{

	ll cost,value,count;

	node(ll cost=0,ll value=0,ll count=0){this->cost=cost,this->value=value,this->count=count;}

	inline friend bool operator < (const node a,const node b){return a.cost*b.value<b.cost*a.value;}

}cs[555];

signed main(){

	ll n,x,d,ans=0;cin>>n>>x>>d;cin>>c[1];sz[1]=1;

	for(int i=2;i<=n;++i) cin>>c[i]>>fa[i],sz[i]=1;

	for(int i=n;i;--i) c[fa[i]]+=c[i],sz[fa[i]]+=sz[i];memset(f,0x3f,sizeof f);f[0]=0;

	for(int i=1;i<=n;++i){

		int T=min(n,d);if(i==1) T=n;

		for(int bit=1;bit<=T;T-=bit,bit<<=1) for(int j=n*n*n;j>=bit*sz[i];--j) f[j]=min(f[j],f[j-bit*sz[i]]+bit*c[i]);//bit>>=1;

		for(int j=n*n*n;j>=T*sz[i];--j) f[j]=min(f[j],f[j-T*sz[i]]+T*c[i]);

		if(i==1) cs[i]=node(c[i],sz[i],1e9);

		else cs[i]=node(c[i],sz[i],max(0ll,d-n));

	}

	sort(cs+1,cs+n+1);

	for(int has=0;has<=n*n*n;++has)if(f[has]<=x){

		int las=x-f[has],res=has;

		for(int i=1;i<=n;++i){

			int now=min(cs[i].count,las/cs[i].cost);

			res+=now*cs[i].value;las-=now*cs[i].cost;

		}

		ans=max(ans,res);

	}

	cout<<ans<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.9999752534105997x):
#include<bits/stdc++.h>

using namespace std;

const int N=55;

int n,x,d,fa[N],c[N*N*N];

struct node

{

	int v,id;

	long long w;

	bool operator<(const node k)const

	{

		return v*k.w>w*k.v;

	}

}a[N];

long long dp[N*N*N],ans;

int main()

{

	ios::sync_with_stdio();

	cin>>n>>x>>d;

	for(int i=1;i<=n;i++)

	{

		cin>>a[i].w;

		if(i>1)

			cin>>fa[i];

		a[i].id=i;

		a[i].v=1;

	}

	for(int i=n;i>=1;i--)

	{

		a[fa[i]].w+=a[i].w;

		a[fa[i]].v+=a[i].v;

	}

	sort(a+1,a+n+1);

	int m=min(n,d);

	for(int j=1;j<=n*n*n;j++)

		dp[j]=1e18;

	for(int i=1;i<=n;i++)

	{

		for(int j=0;j<=n*n*n;j++)

			c[j]=0;

		for(int j=0;j<=n*n*n;j++)

		{

			if(c[j]==m)

				continue;

			if(dp[j+a[i].v]>dp[j]+a[i].w)

			{

				dp[j+a[i].v]=dp[j]+a[i].w;

				c[j+a[i].v]=c[j]+1;

			}

			else if(dp[j+a[i].v]==dp[j]+a[i].w&&c[j+a[i].v]>c[j]+1)

				c[j+a[i].v]=c[j]+1;

		}

	}

	for(int i=0;i<=n*n*n;i++)

	{

		if(dp[i]>x)

			continue;

		long long w=dp[i],v=i;

		for(int j=1;j<=n;j++)

		{

			if(a[j].id==1)

			{

				long long zh=(x-w)/a[j].w;

				w+=zh*a[j].w,v+=zh*a[j].v;

				continue;

			}

			long long zh=min((long long)d-m,(x-w)/a[j].w);

			w+=zh*a[j].w,v+=zh*a[j].v;

		}

		ans=max(ans,v);

	}

	cout<<ans<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define maxn 55

#define LL long long

using namespace std;

int n,m,d,V,lim,ans;

LL f[maxn*maxn*maxn];

struct node{

	int v,id; LL w;

	bool operator < (const node &p)const{return v*p.w>w*p.v;}

}a[maxn];

vector<int>G[maxn];

void dfs(int u){for(int v:G[u]) dfs(v),a[u].v+=a[v].v,a[u].w+=a[v].w;}

void ins(LL v,LL w){for(int i=V;i>=v;i--) f[i]=min(f[i],f[i-v]+w);}

int main()

{

	scanf("%d%d%d%d",&n,&m,&d,&a[1].w),a[1].v=a[1].id=1;

	for(int i=2,x;i<=n;i++) scanf("%d%d",&a[i].w,&x),a[i].v=1,G[x].push_back(i);

	dfs(1);

	memset(f,0x3f,sizeof f),f[0]=0;

	V = n*n*n, lim = min(n,d);

	for(int i=1;i<=n;i++){

		int x=lim;

		for(int j=1;j<=x;x-=j,j<<=1) ins(1ll*j*a[i].v,j*a[i].w);

		if(x) ins(1ll*x*a[i].v,x*a[i].w);

	}

	sort(a+1,a+1+n);

	while(a[n].id!=1) n--;

	for(int i=0;i<=V&&f[i]<=m;i++){

		int v=i,w=f[i];

		for(int j=1;j<n;j++){

			int k=min(1ll*d-lim,(m-w)/a[j].w);

			v+=k*a[j].v,w+=k*a[j].w;

		}

		int k=(m-w)/a[n].w;

		ans=max(ans,v+k*a[n].v);

	}

	printf("%d\n",ans);

}

// Faster C++ solution for the same problem (measured speedup: 2.3163153758067785x):
#include<bits/stdc++.h>

using namespace std;

const int N=55;

int n,x,d,fa[N],c[N*N*N];

struct node

{

	int v,id;

	long long w;

	bool operator<(const node k)const

	{

		return v*k.w>w*k.v;

	}

}a[N];

long long dp[N*N*N],ans;

int main()

{

	ios::sync_with_stdio();

	cin>>n>>x>>d;

	for(int i=1;i<=n;i++)

	{

		cin>>a[i].w;

		if(i>1)

			cin>>fa[i];

		a[i].id=i;

		a[i].v=1;

	}

	for(int i=n;i>=1;i--)

	{

		a[fa[i]].w+=a[i].w;

		a[fa[i]].v+=a[i].v;

	}

	sort(a+1,a+n+1);

	int m=min(n,d);

	for(int j=1;j<=n*n*n;j++)

		dp[j]=1e18;

	for(int i=1;i<=n;i++)

	{

		for(int j=0;j<=n*n*n;j++)

			c[j]=0;

		for(int j=0;j<=n*n*n;j++)

		{

			if(c[j]==m)

				continue;

			if(dp[j+a[i].v]>dp[j]+a[i].w)

			{

				dp[j+a[i].v]=dp[j]+a[i].w;

				c[j+a[i].v]=c[j]+1;

			}

			else if(dp[j+a[i].v]==dp[j]+a[i].w&&c[j+a[i].v]>c[j]+1)

				c[j+a[i].v]=c[j]+1;

		}

	}

	for(int i=0;i<=n*n*n;i++)

	{

		if(dp[i]>x)

			continue;

		long long w=dp[i],v=i;

		for(int j=1;j<=n;j++)

		{

			if(a[j].id==1)

			{

				long long zh=(x-w)/a[j].w;

				w+=zh*a[j].w,v+=zh*a[j].v;

				continue;

			}

			long long zh=min((long long)d-m,(x-w)/a[j].w);

			w+=zh*a[j].w,v+=zh*a[j].v;

		}

		ans=max(ans,v);

	}

	cout<<ans<<endl;

	return 0;

}
// Slower C++ solution:
#include<iostream>

using namespace std;



int col[110][110];

int a[10010];



int main()

{

	ios::sync_with_stdio(false);

	int i,j,m,n,k;

	cin>>n>>m>>k;

	for(i=1;i<=k;i++)

	{

		cin>>a[i];

	}

	k=1;

	for(i=0;i<n;i++)

	{

		if(i&1)

		{

			for(j=m-1;j>=0;j--)

			{

				while(a[k]==0) k++;

				col[i][j]=k;

				a[k]--;

			}

		}

		else

		{

			for(j=0;j<m;j++)

			{

				while(a[k]==0) k++;

				col[i][j]=k;

				a[k]--;

			}

		}

	}

	for(i=0;i<n;i++)

	{

		for(j=0;j<m;j++)

		{

			if(j) cout<<" ";

			cout<<col[i][j]; 

		}

		cout<<"\n";

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.287629584608762x):
#include<stdio.h>

using namespace std;

int p[10010];

int main()

{

    int n,m;

    scanf("%d%d",&n,&m);

    int s;

    scanf("%d",&s);

    int a;

    int statu=0;

    for(int i=1;i<=s;i++)

    {

        scanf("%d",&a);

        for(int j=0;j<a;j++,statu++)

            p[statu]=i;

    }

    for(int i=0;i<n;i++)

    {

        if(i%2==0)

        {

            for(int j=0;j<m-1;j++)

            printf("%d ",p[i*m+j]);

            printf("%d\n",p[(i+1)*m-1]);

        }

        else

        {

            for(int j=m-1;j>0;j--)

                printf("%d ",p[i*m+j]);

            printf("%d\n",p[i*m]);

        }

    }

    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <cstring>

using namespace std;



int mp[110][110];

int sa[10010];

int h, w, n;



int main() {

	while (cin >> h >> w) {

		cin >> n;

		memset(mp, 0, sizeof(mp));

		for (int i = 1; i <= n; i++)

			cin >> sa[i];

		int ii = 0, jj = 0, cc = 1;

		int cas = h * w;

		while (cas--) {

			while (sa[cc] == 0) {

				cc++;

			}

			mp[ii][jj] = cc;

			sa[cc]--;;

			if (jj % 2 == 0) {

				ii++;

				if (ii >= w) {

					ii = w - 1;

					jj++;

				}

			}else {

				ii--;

				if (ii < 0) {

					ii = 0;

					jj++;

				}

			}

		}

		for (int j = 0; j < h; j++) {

			for (int i = 0; i < w; i++) {

				if (i)

					cout << " " << mp[i][j];

				else

					cout << mp[i][j];

			}

			cout << endl;

		}

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.484484456459863x):
#include<stdio.h>

using namespace std;

int p[10010];

int main()

{

    int n,m;

    scanf("%d%d",&n,&m);

    int s;

    scanf("%d",&s);

    int a;

    int statu=0;

    for(int i=1;i<=s;i++)

    {

        scanf("%d",&a);

        for(int j=0;j<a;j++,statu++)

            p[statu]=i;

    }

    for(int i=0;i<n;i++)

    {

        if(i%2==0)

        {

            for(int j=0;j<m-1;j++)

            printf("%d ",p[i*m+j]);

            printf("%d\n",p[(i+1)*m-1]);

        }

        else

        {

            for(int j=m-1;j>0;j--)

                printf("%d ",p[i*m+j]);

            printf("%d\n",p[i*m]);

        }

    }

    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <stdio.h>

using namespace std;



int main()

{

	int h, w, n, c[105][105], a[10005];

	scanf("%d%d%d", &h, &w, &n);

	for(int i = 0; i < n; i++) scanf("%d", &a[i]);

	int cur = 0;

	for(int i = 0; i < h; i++)

	{

		if(i%2)

		{

			for(int j = w - 1; j >= 0; j--)

			{

				while(a[cur] == 0) cur++;//whileorz...

				a[cur]--;

				c[i][j] = cur + 1;

			}

		}

		else

		{

			for(int j = 0; j < w; j++)

			{

				while(a[cur] == 0) cur++;

				a[cur]--;

				c[i][j] = cur + 1;

			}

		}

	}

	for(int i = 0; i < h; i++)

	{

		for(int j = 0; j < w; j++)

		{

			if(j)

                printf(" ");

			printf("%d", c[i][j]);

		}

		puts("");

	}

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.16009118198455x):
#include<stdio.h>

using namespace std;

int p[10010];

int main()

{

    int n,m;

    scanf("%d%d",&n,&m);

    int s;

    scanf("%d",&s);

    int a;

    int statu=0;

    for(int i=1;i<=s;i++)

    {

        scanf("%d",&a);

        for(int j=0;j<a;j++,statu++)

            p[statu]=i;

    }

    for(int i=0;i<n;i++)

    {

        if(i%2==0)

        {

            for(int j=0;j<m-1;j++)

            printf("%d ",p[i*m+j]);

            printf("%d\n",p[(i+1)*m-1]);

        }

        else

        {

            for(int j=m-1;j>0;j--)

                printf("%d ",p[i*m+j]);

            printf("%d\n",p[i*m]);

        }

    }

    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <stdio.h>

using namespace std;



int main()

{

	int h, w, n, c[105][105], a[10005];

	scanf("%d%d%d", &h, &w, &n);

	for(int i = 0; i < n; i++) scanf("%d", &a[i]);

	int cur = 0;

	for(int i = 0; i < h; i++)

	{

		if(i%2)

		{

			for(int j = w - 1; j >= 0; j--)

			{

				if(a[cur] == 0) cur++;

				a[cur]--;

				c[i][j] = cur + 1;

			}

		}

		else

		{

			for(int j = 0; j < w; j++)

			{

				if(a[cur] == 0) cur++;

				a[cur]--;

				c[i][j] = cur + 1;

			}

		}

	}

	for(int i = 0; i < h; i++)

	{

		for(int j = 0; j < w; j++)

		{

			if(j)

                printf(" ");

			printf("%d", c[i][j]);

		}

		puts("");

	}

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.161549777241519x):
#include<stdio.h>

using namespace std;

int p[10010];

int main()

{

    int n,m;

    scanf("%d%d",&n,&m);

    int s;

    scanf("%d",&s);

    int a;

    int statu=0;

    for(int i=1;i<=s;i++)

    {

        scanf("%d",&a);

        for(int j=0;j<a;j++,statu++)

            p[statu]=i;

    }

    for(int i=0;i<n;i++)

    {

        if(i%2==0)

        {

            for(int j=0;j<m-1;j++)

            printf("%d ",p[i*m+j]);

            printf("%d\n",p[(i+1)*m-1]);

        }

        else

        {

            for(int j=m-1;j>0;j--)

                printf("%d ",p[i*m+j]);

            printf("%d\n",p[i*m]);

        }

    }

    return 0;

}
// Slower C++ solution:
#include<iostream>

using namespace std;



int col[110][110];

int a[10010];



int main()

{

	ios::sync_with_stdio(false);

	int i,j,m,n,k;

	cin>>n>>m>>k;

	for(i=1;i<=k;i++)

	{

		cin>>a[i];

	}

	k=1;

	for(i=0;i<n;i++)

	{

		if(i&1)

		{

			for(j=m-1;j>=0;j--)

			{

				while(a[k]==0) k++;

				col[i][j]=k;

				a[k]--;

			}

		}

		else

		{

			for(j=0;j<m;j++)

			{

				while(a[k]==0) k++;

				col[i][j]=k;

				a[k]--;

			}

		}

	}

	for(i=0;i<n;i++)

	{

		for(j=0;j<m;j++)

		{

			if(j) cout<<" ";

			cout<<col[i][j]; 

		}

		cout<<"\n";

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.257808263827152x):
#include<cstdio>



int row, col;

int n, i , j;



int c[105][105];

int num[10005];



int main()

{

	while (~scanf("%d%d", &row, &col))

	{

		int cnt = 1;

		scanf("%d", &n);

		for (i = 1; i <= n; i++) scanf("%d", &num[i]);



		for (i = 1; i <= row; i++)

		{

			if (i & 1)

			{

				for (j = 1; j <= col; j++)

				{

					if (num[cnt])

					{

						c[i][j] = cnt;

						num[cnt]--;

					}

					else

					{

						cnt++;						

						c[i][j] = cnt;

						num[cnt]--;

					}

				}

			}

					

			else

			{

				for (j = col; j >= 1; j--)

				{

					if (num[cnt])

					{

						c[i][j] = cnt;

						num[cnt]--;

					}

					else

					{

						cnt++;						

						c[i][j] = cnt;

						num[cnt]--;

					}

				}

			}

		}



		for (i = 1; i <= row; i++)

		{

			for (j = 1; j <= col; j++)

			{

				printf("%d", c[i][j]);

				if (j != col) printf(" ");

				else printf("\n");

			}

		}

	}

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <cstring>

using namespace std;



int mp[110][110];

int sa[10010];

int h, w, n;



int main() {

	while (cin >> h >> w) {

		cin >> n;

		memset(mp, 0, sizeof(mp));

		for (int i = 1; i <= n; i++)

			cin >> sa[i];

		int ii = 0, jj = 0, cc = 1;

		int cas = h * w;

		while (cas--) {

			while (sa[cc] == 0) {

				cc++;

			}

			mp[ii][jj] = cc;

			sa[cc]--;;

			if (jj % 2 == 0) {

				ii++;

				if (ii >= w) {

					ii = w - 1;

					jj++;

				}

			}else {

				ii--;

				if (ii < 0) {

					ii = 0;

					jj++;

				}

			}

		}

		for (int j = 0; j < h; j++) {

			for (int i = 0; i < w; i++) {

				if (i)

					cout << " " << mp[i][j];

				else

					cout << mp[i][j];

			}

			cout << endl;

		}

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.453552908082511x):
#include<cstdio>



int row, col;

int n, i , j;



int c[105][105];

int num[10005];



int main()

{

	while (~scanf("%d%d", &row, &col))

	{

		int cnt = 1;

		scanf("%d", &n);

		for (i = 1; i <= n; i++) scanf("%d", &num[i]);



		for (i = 1; i <= row; i++)

		{

			if (i & 1)

			{

				for (j = 1; j <= col; j++)

				{

					if (num[cnt])

					{

						c[i][j] = cnt;

						num[cnt]--;

					}

					else

					{

						cnt++;						

						c[i][j] = cnt;

						num[cnt]--;

					}

				}

			}

					

			else

			{

				for (j = col; j >= 1; j--)

				{

					if (num[cnt])

					{

						c[i][j] = cnt;

						num[cnt]--;

					}

					else

					{

						cnt++;						

						c[i][j] = cnt;

						num[cnt]--;

					}

				}

			}

		}



		for (i = 1; i <= row; i++)

		{

			for (j = 1; j <= col; j++)

			{

				printf("%d", c[i][j]);

				if (j != col) printf(" ");

				else printf("\n");

			}

		}

	}

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <stdio.h>

using namespace std;



int main()

{

	int h, w, n, c[105][105], a[10005];

	scanf("%d%d%d", &h, &w, &n);

	for(int i = 0; i < n; i++) scanf("%d", &a[i]);

	int cur = 0;

	for(int i = 0; i < h; i++)

	{

		if(i%2)

		{

			for(int j = w - 1; j >= 0; j--)

			{

				while(a[cur] == 0) cur++;//whileorz...

				a[cur]--;

				c[i][j] = cur + 1;

			}

		}

		else

		{

			for(int j = 0; j < w; j++)

			{

				while(a[cur] == 0) cur++;

				a[cur]--;

				c[i][j] = cur + 1;

			}

		}

	}

	for(int i = 0; i < h; i++)

	{

		for(int j = 0; j < w; j++)

		{

			if(j)

                printf(" ");

			printf("%d", c[i][j]);

		}

		puts("");

	}

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.130989155842583x):
#include<cstdio>



int row, col;

int n, i , j;



int c[105][105];

int num[10005];



int main()

{

	while (~scanf("%d%d", &row, &col))

	{

		int cnt = 1;

		scanf("%d", &n);

		for (i = 1; i <= n; i++) scanf("%d", &num[i]);



		for (i = 1; i <= row; i++)

		{

			if (i & 1)

			{

				for (j = 1; j <= col; j++)

				{

					if (num[cnt])

					{

						c[i][j] = cnt;

						num[cnt]--;

					}

					else

					{

						cnt++;						

						c[i][j] = cnt;

						num[cnt]--;

					}

				}

			}

					

			else

			{

				for (j = col; j >= 1; j--)

				{

					if (num[cnt])

					{

						c[i][j] = cnt;

						num[cnt]--;

					}

					else

					{

						cnt++;						

						c[i][j] = cnt;

						num[cnt]--;

					}

				}

			}

		}



		for (i = 1; i <= row; i++)

		{

			for (j = 1; j <= col; j++)

			{

				printf("%d", c[i][j]);

				if (j != col) printf(" ");

				else printf("\n");

			}

		}

	}

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <stdio.h>

using namespace std;



int main()

{

	int h, w, n, c[105][105], a[10005];

	scanf("%d%d%d", &h, &w, &n);

	for(int i = 0; i < n; i++) scanf("%d", &a[i]);

	int cur = 0;

	for(int i = 0; i < h; i++)

	{

		if(i%2)

		{

			for(int j = w - 1; j >= 0; j--)

			{

				if(a[cur] == 0) cur++;

				a[cur]--;

				c[i][j] = cur + 1;

			}

		}

		else

		{

			for(int j = 0; j < w; j++)

			{

				if(a[cur] == 0) cur++;

				a[cur]--;

				c[i][j] = cur + 1;

			}

		}

	}

	for(int i = 0; i < h; i++)

	{

		for(int j = 0; j < w; j++)

		{

			if(j)

                printf(" ");

			printf("%d", c[i][j]);

		}

		puts("");

	}

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.132439524873348x):
#include<cstdio>



int row, col;

int n, i , j;



int c[105][105];

int num[10005];



int main()

{

	while (~scanf("%d%d", &row, &col))

	{

		int cnt = 1;

		scanf("%d", &n);

		for (i = 1; i <= n; i++) scanf("%d", &num[i]);



		for (i = 1; i <= row; i++)

		{

			if (i & 1)

			{

				for (j = 1; j <= col; j++)

				{

					if (num[cnt])

					{

						c[i][j] = cnt;

						num[cnt]--;

					}

					else

					{

						cnt++;						

						c[i][j] = cnt;

						num[cnt]--;

					}

				}

			}

					

			else

			{

				for (j = col; j >= 1; j--)

				{

					if (num[cnt])

					{

						c[i][j] = cnt;

						num[cnt]--;

					}

					else

					{

						cnt++;						

						c[i][j] = cnt;

						num[cnt]--;

					}

				}

			}

		}



		for (i = 1; i <= row; i++)

		{

			for (j = 1; j <= col; j++)

			{

				printf("%d", c[i][j]);

				if (j != col) printf(" ");

				else printf("\n");

			}

		}

	}

	return 0;

}