[codejam] ant stack

Link to the problem

I think this is a good dynamic programming problem for exercise🤔.
One tricky point is that we can find the maximum number of ants that we can achieve when weight $ leq 10^{9} $ is around 138, which is much smaller than $10^{5}$.
dp[i][j] means the minimum weight when we use ants from 1 to i to get a pile containing j ants. And the transition function is like
dp[i][j] = min( dp[i-1][j], dp[i-1][j-1] + weights[i] (if 6*weights[i] $geq$ dp[i-1][j-1]))

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


#include <algorithm>
#include <set>
#include <math.h>
#include <string>
#include <set>
#include <map>
#include <cstring>
using namespace std;
#define ll long long


const int maxn = 100005;
const int maxant = 150;
ll weights[maxn];
ll dp[maxn][maxant+5];

int ()
{
int T;
scanf("%d",&T);
int N;
for(int t = 1; t <= T;t++)
{
printf("Case #%d: ",t);
scanf("%d",&N);

for(int i = 0 ;i <= N;i++)
for(int j = 0; j <= maxant;j++)
{
dp[i][j] = 0x3f3f3f3f3f3f3f3f;
}

for(int i = 1;i <= N;i++)
{
scanf("%lld",&weights[i]);
}
for(int i = 0; i<= N;i++)
{
dp[i][0] =0;
}
for(int i = 1;i <= N;i++)
{
for(int j = 1;j <= maxant;j++)
{
if(dp[i][j] > dp[i-1][j])
{
dp[i][j] = dp[i-1][j];
}
if(6*weights[i] >= dp[i-1][j-1])
{
if(dp[i][j] > dp[i-1][j-1] + weights[i])
dp[i][j] = dp[i-1][j-1] + weights[i];

}
}
}
for(int i = maxant;i >= 1;i--)
{
if(dp[N][i] != 0x3f3f3f3f3f3f3f3f)
{
printf("%dn",i);
break;
}

}

}

return 0;
}