贪心 + 模拟
预处理出最便宜的原材料价格,然后用map储存每种价格的电脑数量。
这里因为每个月价格都在变化,所以可以用相对价格,也就是减去一个sigema(e[i])。
每次选择最小的卖,统计贡献,最后如果超出库存,反着删除即可(相当于没有生产这些电脑)
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline ll read(){
ll ret = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) ret = (ret << 3) + (ret << 1) + (ch ^ 48), ch = getchar();
return w ? -ret : ret;
}
inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template <typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template <typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template <typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int N = 50005;
ll _, k, c[N], d[N], m[N], p[N], e[N], R[N], E[N];
map<ll, ll> record;
int main(){
for(_ = read(); _; _ --){
k = read(), record.clear();
for(int i = 1; i <= k; i ++){
c[i] = read(), d[i] = read(), m[i] = read(), p[i] = read();
}
for(int i = 1; i < k; i ++){
e[i] = read(), R[i] = read(), E[i] = read();
}
for(int i = 1; i < k; i ++){
if(c[i] + R[i] < c[i + 1]) c[i + 1] = c[i] + R[i];
}
ll ans = 0;
int sum = 0, tot = 0;
for(int i = 1; i <= k; i ++){
record[c[i] + m[i] - sum] += p[i], tot += p[i];
while(!record.empty() && d[i]){
if(record.begin()->second > d[i]){
tot -= d[i];
record.begin()->second -= d[i];
ans += (record.begin()->first + sum) * d[i];
d[i] = 0;
}
else{
tot -= record.begin()->second;
d[i] -= record.begin()->second;
ans += (record.begin()->first + sum) * record.begin()->second;
record.erase(record.begin()->first);
}
}
if(d[i]){
ans = -1;
break;
}
if(tot > e[i]){
while(!record.empty() && tot > e[i]){
if(tot - record.rbegin()->second >= e[i]){
tot -= record.rbegin()->second;
record.erase(record.rbegin()->first);
}
else{
record.rbegin()->second -= (tot - e[i]);
tot = e[i];
}
}
}
sum += E[i];
}
printf("%lld\n", ans);
}
return 0;
}