简单dp
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include
<
iostream
>
#include < cstdlib >
#include < cstring >
#include < cstdio >
using namespace std;
int n, even, odd;
int main()
{
// freopen("t.txt", "r", stdin);
scanf( " %d " , & n);
if (n == 1 )
{
int a;
scanf( " %d " , & a);
printf( " %d\n " , a);
return 0 ;
}
scanf( " %d%d " , & odd, & even);
int a, temp;
temp = even;
even = odd - even;
odd = max(temp, odd);
for ( int i = 2 ; i < n; i ++ )
{
scanf( " %d " , & a);
temp = max(odd, even + a);
even = max(even, odd - a);
odd = temp;
}
printf( " %d\n " , max(even, odd));
return 0 ;
}
#include < cstdlib >
#include < cstring >
#include < cstdio >
using namespace std;
int n, even, odd;
int main()
{
// freopen("t.txt", "r", stdin);
scanf( " %d " , & n);
if (n == 1 )
{
int a;
scanf( " %d " , & a);
printf( " %d\n " , a);
return 0 ;
}
scanf( " %d%d " , & odd, & even);
int a, temp;
temp = even;
even = odd - even;
odd = max(temp, odd);
for ( int i = 2 ; i < n; i ++ )
{
scanf( " %d " , & a);
temp = max(odd, even + a);
even = max(even, odd - a);
odd = temp;
}
printf( " %d\n " , max(even, odd));
return 0 ;
}