# hdu1087

http://acm.hdu.edu.cn/showproblem.php?pid=1087

View Code

// dp问题：
// 状态转移方程 b[i]=max(b[i], b[j]+a[i]);
#include " iostream "
using namespace std;
#define M 1010
int n;
int a[M],b[M];
int i,j;
int main()
{
while (cin >> n,n)
{
int Max =- 0xffff ;
memset(b,
0 , sizeof ( 0 ));
for (i = 0 ;i < n;i ++ )
cin
>> a[i];

b[
0 ] = a[ 0 ];
for (i = 1 ;i < n;i ++ )
{
b[i]
= a[i];
for (j = 0 ;j < i;j ++ )
if (a[j] < a[i] && b[i] < b[j] + a[i])
b[i]
= b[j] + a[i];
if (b[i] > Max) Max = b[i];
}
cout
<< Max << endl;
}
return 0 ;
}
dfs(tle)

#include " iostream "
using namespace std;
int a[ 1010 ];
int n,sum;
int p = 0 ;
void dfs( int x, int y)
{
while (y <= x && x < n) y ++ ;

if (a[x] < a[y] && y < n && x < n)
{
sum
+= a[y];
x
= y;
if (x < n - 1 && y < n - 1 ) dfs(x,y);
}
else if (y < n - 1 ){ y ++ ; if (y < n - 1 ) dfs(x,y);}
}
int main()
{
int i,j;
while (cin >> n,n)
{
for (i = 0 ;i < n;i ++ ) cin >> a[i];
int Max =- 1 ;
sum
= 0 ;
for (i = 0 ;i < n;i ++ )
{
sum
= a[i];
if (Max < sum) Max = sum;
for (j = i + 1 ; j < n; j ++ )
{
p
= 0 ;
if (a[i] < a[j])
{
sum
+= a[j];
dfs(j,p);
}
if (sum > Max) Max = sum;
sum
= a[i];
}
}
cout
<< Max << endl;
}
return 0 ;
}

10-30 414
07-15 1046

12-03 505
12-11 18
03-15 278
07-15 215
11-26 246