-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRICKDP.cpp
More file actions
82 lines (80 loc) · 1.4 KB
/
CRICKDP.cpp
File metadata and controls
82 lines (80 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<bits/stdc++.h>
using namespace std;
vector<long long int> val;
vector<long long int> cost;
vector<long long int> a;
long long int K[100005][505];
int main() {
int t;
scanf("%d",&t);
while(t--)
{
long long int sum=0;
int n,m,k;
a.clear();
scanf("%d %d %d",&n,&k,&m);
for(int i=0;i<n;i++)
{
long long int num;
scanf("%lld",&num);
a.push_back(num);
sum+=a[i];
}
vector<int> dp[100002]={};
for(int i=0;i<m;i++)
{
int l,r,c;
scanf("%d %d %d",&l,&r,&c);
l--;
r--;
dp[l].push_back(c);
dp[r+1].push_back(-1*c);
}
val.clear();
cost.clear();
set<int> s;
map<int,int> ma;
for(int i=0;i<n;i++)
{
if(dp[i].size()!=0)
{
for(int j=0;j<dp[i].size();j++)
{
if(dp[i][j]>0)
{
s.insert(dp[i][j]);
ma[dp[i][j]]++;
}
else
{
ma[-1*dp[i][j]]--;
if(ma[-1*dp[i][j]]==0)
s.erase(-1*dp[i][j]);
}
}
}
if(s.size()!=0)
{
if(a[i]<0)
{
val.push_back(a[i]);
cost.push_back(*s.begin());
}
}
}
for(int i=0;i<=val.size();i++)
{
for(int w=0;w<=k;w++)
{
if (i==0 || w==0)
K[i][w]=0;
else if(cost[i-1]<=w)
K[i][w]=min(val[i-1]+K[i-1][w-cost[i-1]],K[i-1][w]);
else
K[i][w]=K[i-1][w];
}
}
printf("%lld\n",(sum-K[val.size()][k]));
}
return 0;
}