#include <algorithm>
#include <iostream>
#include <cstdio>
#include <queue>
#include <deque>
using namespace std;
#define debug(x) cerr << #x << "=" << x << endl;
struct craft{
int s, l, p;
craft(){}
craft(int l,int p,int s) : l(l), p(p), s(s) {}
}cra[110];
deque <int> q;
bool cmp(craft a, craft b) {
return a.s < b.s;
}
void empty() {
while(!q.empty()) {
q.pop_front();
}
}
int n,m,ans,f[110][16100];
int calc(int i, int p, int k) {
return f[i-1][k] - p*k;
}
int main() {
cin >> n >> m;
for(int i=1; i<=m; i++) {
int l,p,s;
cin >> l >> p >> s;
cra[i] = craft(l,p,s);
}
sort(cra+1,cra+m+1,cmp);
for(int i=1; i<=m; i++) {
int l = cra[i].l, s = cra[i].s, p = cra[i].p;
empty();
for(int k=max(0,cra[i].s - cra[i].l); k<cra[i].s; k++) {
while(!q.empty() && calc(i, p, k) >= calc(i, p, q.back())) q.pop_back();
q.push_back(k);
}
for(int j=1; j<=n; j++) {
f[i][j] = max(f[i-1][j], f[i][j-1]);
if(j >= s) {
while(!q.empty() && q.front() < j - l) q.pop_front();
if(!q.empty()) f[i][j] = max(f[i][j], j * p + calc(i, p, q.front()));
}
}
}
cout << f[m][n] << endl;
return 0;
}
POJ1821 Fence - 单调队列DP
最新推荐文章于 2020-04-09 14:32:44 发布