-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathoptim.cpp
47 lines (46 loc) · 1.05 KB
/
optim.cpp
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
# include <fstream>
# include <algorithm>
# define inf 2000000000
using namespace std;
ifstream f("optim.in");
ofstream g("optim.out");
int i,j,n,k,minn,maxx,p,t;
int a[100], m[100][100], M[100][100];
int main ()
{
f>>n>>k;
for (i=1; i<=n; ++i)
f>>a[i];
for (i=1; i<=n; ++i)
{
m[i][0]=m[i-1][0]+a[i];
M[i][0]=M[i-1][0]+a[i];
for (j=1; j<i && j<=k; ++j)
{
if (j<i-1)
{
minn = m[i-1][j] + a[i];
maxx = M[i-1][j] + a[i];
}
else
{
minn = inf;
maxx = -inf;
}
p=a[i];
for (t=1; t<=j; ++t)
{
p*=a[i-t];
if (i-1 > j || (i-t-1 == 0))
{
minn=min(minn, m[i-t-1][j-t]+p);
maxx=max(maxx, M[i-t-1][j-t]+p);
}
}
m[i][j]=minn;
M[i][j]=maxx;
}
}
g<<m[n][k]<<" "<<M[n][k]<<"\n";
return 0;
}