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<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef pair<ll, ll> LP;
typedef vector<LP> vec;
typedef vector<string> svec;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;


void solve() {
	int p, r, t; cin >> p >> r >> t;
	vector<int> l(r);
	rep(i, r)cin >> l[i];
	vector<vector<int>> n(p);
	rep(i, p) {
		n[i].resize(r);
		rep(j, r)cin >> n[i][j];
	}
	vector<P> v(t);
	rep(i, t) {
		cin >> v[i].first >> v[i].second; v[i].first--; v[i].second--;
	}
	int le = 0, ri = t + 1;
	while (ri - le > 1) {
		int m = (le + ri) / 2;
		vector<vector<int>> c(p);
		rep(i, p) {
			c[i].resize(r, 0);
		}
		vector<int> rest = l;
		rep(i, m) {
			c[v[i].first][v[i].second]++;
			rest[v[i].second]--;
		}
		rep(i, p) {
			int d = mod;
			rep(j, r) {
				if (n[i][j] == 0)continue;
				d = min(d, c[i][j] / n[i][j]);
			}
			if (d > 0) {
				rep(j, r) {
					c[i][j] -= d * n[i][j];
					rest[j] += d * n[i][j];
				}
			}
		}
		vector<int> num(p, 0);
		vector<vector<P>> v(r);
		rep(i, p) {
			bool exi1 = false;
			rep(j, r) {
				if (c[i][j] > 0)exi1 = true;
			}
			if (!exi1) {
				num[i] = r; continue;
			}
			rep(j, r) {
				if (n[i][j] == c[i][j]) {
					num[i]++;
				}
				else {
					v[j].push_back({ n[i][j] - c[i][j],i });
				}
			}
		}
		rep(j, r)sort(v[j].begin(), v[j].end());
		vector<int> lid(r, 0);
		while (true) {
			vector<int> nex;
			rep(j, r) {
				while (lid[j] < v[j].size() && v[j][lid[j]].first <= rest[j]) {
					num[v[j][lid[j]].second]++;
					if (num[v[j][lid[j]].second] == r) {
						nex.push_back(v[j][lid[j]].second);
					}
					lid[j]++;
				}
			}
			if (nex.empty())break;
			rep(i, nex.size()) {
				rep(j, r) {
					rest[j] += c[nex[i]][j];
				}
			}
		}
		bool f = true;
		rep(j, p) {
			if (num[j] != r)f = false;
		}
		//cout << m << " " << f << endl;
		if (f) {
			le = m;
		}
		else {
			ri = m;
		}
	}
	if (ri == t + 1)ri = -1;
	cout << ri << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	//cout << fixed << setprecision(10);
	//init();
	solve();
	//stop
		return 0;
}