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;

int P, R, T;

int res[300];
int need[300][300];
int rlog[200000][2];

bool check(int t){
	int state[300][300];
	for(int i = 0; i < P; i++){
		for(int j = 0; j < R; j++){
			state[i][j] = 0;
		}
	}
	int curres[300];
	for(int i = 0; i < R; i++){
		curres[i] = res[i];
	}

	for(int i = 0; i < t; i++){
		int p = rlog[i][0];
		int r = rlog[i][1];
		state[p][r]++;
		curres[r]--;
		if(state[p][r] == need[p][r]){
			bool flg = true;
			for(int j = 0; j < R; j++){
				if(state[p][j] < need[p][j]){
					flg = false;
					break;
				}
			}
			if(flg){
				for(int j = 0; j < R; j++){
					curres[j] += state[p][j];
					state[p][j] = 0;
				}
			}
		}
	}

	bool fin[300];
	for(int i = 0; i < P; i++){
		fin[i] = false;
	}
	bool flg = true;
	while(flg){
		flg = false;
		for(int i = 0; i < P; i++){
			if(!fin[i]){
				bool f2 = true;;
				for(int j = 0; j < R; j++){
					if(need[i][j] - state[i][j] > curres[j]){
						f2 = false;
						break;
					}
				}
				if(f2){
					for(int j = 0; j < R; j++){
						curres[j] += state[i][j];
						state[i][j] = 0;
					}
					fin[i] = true;
					flg = true;
				}
			}
		}
	}
	for(int i = 0; i < P; i++){
		if(!fin[i]) return false;
	}
	return true;
}


int main(){
	cin >> P >> R >> T;
	for(int i = 0; i < R; i++){
		cin >> res[i];
	}

	for(int i = 0; i < P; i++){
		for(int j = 0; j < R; j++){
			cin >> need[i][j];
		}
	}

	for(int i = 0; i < T; i++){
		cin >> rlog[i][0] >> rlog[i][1];
		rlog[i][0]--;
		rlog[i][1]--;
	}

	
	int l = 0;
	int r = T + 2;
	while(r - l > 1){
		int m = (l + r) / 2;
		if(check(m)){
			l = m;
		}else{
			r = m;
		}
	}
	if(l > T){
		cout << -1 << endl;
	}else{
		cout << (l+1) << endl;
	}
}