Problem: Example

Input

2 2 7
3 4
3 2
1 3
1 1
2 2
1 2
2 1
1 2
2 2
1 1


Output

5

Solution (in CPP):

Solution:
#include<bits/stdc++.h>
using namespace std;
const int maxn=311,maxt=200011;
int n,m,t,s[maxn],w[maxn][maxn],a[maxt],b[maxt];
void init(){
    scanf("%d%d%d",&n,&m,&t);
    for (int i=1;i<=m;++i) scanf("%d",s+i);
    for (int i=1;i<=n;++i) for (int j=1;j<=m;++j) scanf("%d",&w[i][j]);
    for (int i=1;i<=t;++i) scanf("%d%d",a+i,b+i);
}
bool check(int t){
    static int sx[maxn]; memcpy(sx,s,sizeof(s));
    static int wx[maxn][maxn]; memset(wx,0,sizeof(w));
    for (int i=1;i<=t;++i){
        --sx[b[i]]; ++wx[a[i]][b[i]];
    }
    static bool vis[maxn]; memset(vis,0,sizeof(vis));
    for (int t=1;t<=n;++t){
        for (int i=1;i<=n;++i) if (!vis[i]){
			bool flag=1;
			for (int j=1;j<=m;++j) if (wx[i][j]+max(0,sx[j])<w[i][j]){flag=0; break;}
            if (flag){
				vis[i]=1;
				for (int j=1;j<=m;++j) sx[j]+=wx[i][j];
            }
        }
    }
	for (int i=1;i<=n;++i) if (!vis[i]) return true;
	return false;
}
void work(){
    int t=1,w=::t;
    while (t<=w){
		int mid=(t+w)>>1;
		if (check(mid)) w=mid-1;else t=mid+1;
    }
    printf("%d\n",w==::t?-1:w+1);
}
int main(){
	init();
	work();
	return 0;
}