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>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 300

struct Info{
	int process,request;
};
int N,num_resource,T;
int num[NUM],available_num[NUM];
int need[NUM][NUM],tmp_have[NUM][NUM];
int gather_num[NUM];
bool finished[NUM];
Info info[200000];

bool is_lock(int tmp_time){

	for(int i = 0; i < N; i++){
		finished[i] = false;
		gather_num[i] = 0;
	}

	for(int i = 0; i < num_resource; i++){
		available_num[i] = num[i];
	}

	for(int i = 0; i < N; i++){
		for(int k = 0; k < num_resource; k++){
			tmp_have[i][k] = 0;
			if(need[i][k] == 0){
				gather_num[i]++;
			}
		}
	}

	int remain_num = N;
	int proc,req;

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

		if(available_num[info[i].request] == 0)continue;

		proc = info[i].process;
		req = info[i].request;

		tmp_have[proc][req]++;
		available_num[req]--;

		if(tmp_have[proc][req] == need[proc][req]){
			gather_num[proc]++;

			if(gather_num[proc] == num_resource){

				finished[proc] = true;
				remain_num--;
				for(int k = 0; k < num_resource; k++){
					available_num[k] += tmp_have[proc][k];
				}
			}
		}
	}

	bool FLG,can_finish;

	while(true){

		FLG = false;

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

			if(finished[i])continue;

			can_finish = true;
			for(int k = 0; k < num_resource; k++){
				if(available_num[k] < need[i][k]-tmp_have[i][k]){
					can_finish = false;
					break;
				}
			}
			if(can_finish){
				finished[i] = true;
				FLG = true;
				remain_num--;
				for(int k = 0; k < num_resource; k++){
					available_num[k] += tmp_have[i][k];
				}
			}
		}
		if(!FLG)break;
	}
	return remain_num > 0;
}

int main(){

	scanf("%d %d %d",&N,&num_resource,&T);

	for(int i = 0; i < num_resource; i++){
		scanf("%d",&num[i]);
	}

	for(int i = 0; i < N; i++){
		for(int k = 0; k < num_resource; k++){
			scanf("%d",&need[i][k]);
		}
	}

	for(int i = 0; i < T; i++){
		scanf("%d %d",&info[i].process,&info[i].request);
		info[i].process--;
		info[i].request--;
	}

	int left = 0,right = T,m = (left+right)/2;
	int ans = -1;

	while(left <= right){

		if(is_lock(m)){
			ans = m;
			right = m-1;
		}else{
			left = m+1;
		}
		m = (left+right)/2;
	}

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

	return 0;
}