Hello everyone! I have trouble understanding why my solution to the aforementioned problem fails (But all tests that I did passed including the sample case) . Any help from any good Samaritan is much appreciated!
#include "bits/stdc++.h"
using namespace std;
using namespace std::chrono;
using ll = long long;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define nl '\n'
// #define RUNTIME
#define OJ_FileIO
#ifdef OJ_FileIO
void setIO(string s)
{
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
#endif
void solve()
{
ll N, M, R;
cin >> N >> M >> R;
vector<ll> cows(N);
for (ll i = 0; i < N; i++)
{
cin >> cows[i];
}
vector<pair<ll, ll>> shops(M);
for (ll i = 0; i < M; i++)
{
cin >> shops[i].second >> shops[i].first;
}
vector<ll> rentals(R);
for (ll i = 0; i < R; i++)
{
cin >> rentals[i];
}
sort(rall(cows));
sort(rall(shops));
sort(all(rentals));
vector<ll> prefix(R + 1);
for (ll i = 1; i <= R; i++)
{
prefix[i] = prefix[i - 1] + rentals[i - 1];
}
ll mx = INT64_MIN;
ll last = 0;
for (ll i = 0; i <= N; i++)
{
ll milkmoney = 0;
ll rentalmoney = 0;
vector<pair<ll, ll>> ishops = shops;
vector<ll> icows = cows;
ll milked;
auto milk_the_cows = [&]()
{
ll litres = icows[milked - 1];
ll capable = min(litres, ishops[last].second);
milkmoney += capable * shops[last].first;
// cout << last << nl;
ishops[last].second -= capable;
icows[milked - 1] -= capable;
};
for (milked = 1; milked <= i; milked++)
{
milk_the_cows();
if (ishops[last].second == 0)
{
last++;
last = min(last, M - 1);
milk_the_cows();
}
}
// cout << milkmoney << " ";
rentalmoney = prefix[R] - prefix[max(R - (N - i), 0LL)];
// cout << rentalmoney << nl;
mx = max(mx, rentalmoney + milkmoney);
}
cout << mx;
}
int main()
{
auto start = high_resolution_clock::now();
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef OJ_FileIO
setIO("prob");
#endif
solve();
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
#ifdef RUNTIME
cout << "\n"
<< "Run time : " << duration.count() << " microseconds";
#endif
return 0;
}