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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <functional>

using namespace std;
typedef long long ll;
typedef pair<int, int> Pi;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define sz(x) (int)x.size()
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) x.begin(),x.end()

int p, r, t;
int L[320], tL[320], R[302][302], tR[302][302];
int P[200020][2];
int End[320];

bool is_dead(int tim){
	rep(i, p)End[i] = 0;
	rep(i, p){
		rep(j, r)tR[i][j] = R[i][j];
	}
	rep(i, r)tL[i] = L[i];
	
	for(int i=0;i<tim;i++){
		int x = P[i][0], y = P[i][1];
		tR[x][y]--, tL[y]--;
	}
	rep(i, p){
		int c = 0;
		for(c=0;c<r;c++)if(tR[i][c] != 0)break;
		
		if(c == r){
			rep(j, r)tL[j] += R[i][j];
			End[i] = 1;
		}
	}
	
	
	while(1){
		int changed = 0;
		rep(i, p){
			if(End[i])continue;
			int c;
			for(c=0;c<r;c++)if(tL[c] < tR[i][c])break;
			
			if(c == r){
				rep(j, r){
					tL[j] += R[i][j] - tR[i][j];
				}
				End[i] = 1;
				changed = 1;
			}
		}
		if(changed == 0)break;
	}
	
	//rep(i, p)printf("%d", End[i]);
	
	bool res = 0;
	rep(i, p)res |= !End[i];
	return res;
}

void solve(int tc){
	scanf("%d%d%d", &p, &r, &t);
	for(int i=0;i<r;i++)scanf("%d", L+i);
	for(int i=0;i<p;i++){
		for(int j=0;j<r;j++)scanf("%d", R[i]+j);
	}
	for(int i=0;i<t;i++)scanf("%d%d", P[i], P[i]+1);
	for(int i=0;i<t;i++)P[i][0]--, P[i][1]--;
	
	int low = 1, high = t, ans = -1;
	while(low <= high){
		int mid = (low + high) / 2;
		if(is_dead(mid))ans = mid, high = mid - 1;
		else low = mid + 1;
	}
	printf("%d\n", ans);
}

int main(){
	int Tc = 1;// scanf("%d",&Tc);
	for(int i=1;i<=Tc;i++){
		solve(i);
	}
	return 0;
}