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;
#define int long long
int p,r,t;
int l[333];
int n[333][333];
int P[214514],R[214514];
vector<int> v[333][333];
bool check(int T){
  int x[333][333]={},y[333][333]={},z[333]={};
  for(int j=0;j<r;j++) z[j]=l[j];
  int cnt=0;
  bool used[333]={};
  for(int i=0;i<p;i++){
    bool flg=1;
    for(int j=0;j<r;j++){
      //for(int k:v[i][j]) cout<<"-"<<k;cout<<endl;
      int k=v[i][j].end()-upper_bound(v[i][j].begin(),v[i][j].end(),T);
      //cout<<T<<":"<<*upper_bound(v[i][j].begin(),v[i][j].end(),T)<<endl;
      x[i][j]=v[i][j].size()-k;
      y[i][j]=n[i][j]-x[i][j];
      //cout<<i<<" "<<j<<":"<<x[i][j]<<" "<<y[i][j]<<endl;
      z[j]-=x[i][j];
      flg&=x[i][j]==n[i][j];
    }
    if(flg){
      for(int j=0;j<r;j++)
	z[j]+=x[i][j];
      used[i]=1;
      cnt++;
    }
  }
  //for(int j=0;j<r;j++) cout<<z[j]<<" ";cout<<endl;
  while(1){
    bool flg=0;
    for(int i=0;i<p;i++){
      if(used[i]) continue;
      bool tmp=1;
      for(int j=0;j<r;j++)
	tmp&=y[i][j]<=z[j];
      if(!tmp) continue;
      flg=used[i]=1;cnt++;
      for(int j=0;j<r;j++) z[j]+=x[i][j];
    }
    if(!flg) break;
  }
  //cout<<T<<":"<<cnt<<" "<<p<<endl;
  return cnt==p;
}
signed main(){
  scanf("%lld %lld %lld",&p,&r,&t);
  for(int i=0;i<r;i++) scanf("%lld",l+i);
  for(int i=0;i<p;i++)
    for(int j=0;j<r;j++)
      scanf("%lld",n[i]+j);
  for(int i=0;i<t;i++) scanf("%lld %lld",P+i,R+i);
  for(int i=0;i<t;i++){
    P[i]--;R[i]--;
    v[P[i]][R[i]].push_back(i+1);
  }
  if(check(t+1)){
    puts("-1");
    return 0;
  }
  int X=0,Y=t+1;
  while(X+1<Y){
    int Z=(X+Y)/2;
    if(check(Z)) X=Z;
    else Y=Z;
  }
  printf("%lld\n",Y);
  return 0;
}