This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
const int MAXN = 2e5 + 13;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
int N, Q;
ll A[MAXN], B[MAXN];
int32_t main()
{
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N >> Q;
FOR(i, 0, N)
{
cin >> A[i];
}
FOR(i, 0, N)
{
cin >> B[i];
}
while(Q--)
{
int l, r; ll k, ans = 0;
cin >> l >> r >> k;
l--; r -= 2;
ll c = 0;
//cost, how much you have of it.
set<pll> s; ll tot = 0;
FOR(i, l, r + 1)
{
ll cnt = A[i], cost = B[i];
if (cnt > k)
{
ans = -1;
break;
}
s.insert({cost, k});
tot += k;
//find out minimum # of money it takes to raise cnt.
while(cnt > 0)
{
auto p = *s.begin(); s.erase(s.begin());
ll num = min(p.se, cnt);
cnt -= num;
tot -= num;
p.se -= num;
if (p.se != 0)
{
s.insert(p);
}
// cerr << "+= " << p.fi << '*' << num << endl;
ans += p.fi * num;
}
cnt = k - A[i];
while(tot > cnt)
{
auto p = *prev(s.end()); s.erase(prev(s.end()));
ll num = min(p.se, tot - cnt);
tot -= num;
p.se -= num;
if (p.se != 0)
{
s.insert(p);
}
}
//after this collection, you're only allowed to have k-cnt.
// cerr << "cnt " << cnt << " cost " << cost << endl;
// if (cnt > k)
// {
// ans = -1;
// break;
// }
// c += cnt;
// //you have to pay off the excess over k.
// while(c > k)
// {
// auto p = *s.begin();
// s.erase(s.begin());
// ll take = min(c - k, p.fi);
// ans += p.fi * take;
// c -= take;
// p.se -= take;
// if (p.se > 0) s.insert(p);
// }
// s.insert({cost, c});
//now cost has to be > k?
}
cout << ans << '\n';
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int32_t main()':
Main.cpp:61:12: warning: unused variable 'c' [-Wunused-variable]
61 | ll c = 0;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |