Comments

Hey can you please check my code I applied your logic it's getting WA in only 2 test cases! Here is the code:(https://atcoder.jp/contests/abc172/submissions/14823164)

Totally it helps! Thank you so very much! Sorry for the late reply :)

Ahh, so that means greedy will not give the optimal solution right?! Any help on how to solve the problem? The English editorial is going to take some days to come :(

Here is my solution to C.

#include <bits/stdc++.h>
using namespace std;

int32_t main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	long long n, m, k;
	cin >> n >> m >> k;
	vector <long long> a(n), b(m);
	for (auto &i : a) cin >> i;
	for (auto &i : b) cin >> i;
	long long i=0LL, j=0LL, count = 0LL;
	while (i<n && j<m) {
		if(a[i] <= b[j]) {
			k -= a[i++];
		}
		else {
			k -= b[j++];
		}
		if (k >= 0LL)
			++count;
		else break;
	}
	bool state = true;
	while (i<n) {
		k -= a[i++];
		if (k >= 0LL)
			++count;
		else {
			state = false;
			break;
		}
	}
	if (state) {
		while (j < m) {
			k -= b[j++];
			if (k >= 0LL)
				++count;
			else break;
		}
	}
	cout << count << '\n';
}

Can anyone tell me why the above code gets WA in 10 test cases. Isn't the problem solvable by 2 pointer kinda technique?