题目描述
There is a row of n coins whose values are some positive integers c₁, c₂,...,cn, not necessarily distinct. The goal is to pick up the maximum amount of money subject to the constraint that no two coins adjacent in the initial row can be picked up.
输入
Two lines, the first line is n (0< n <=10000), and the second line is value of coin(0< value <= 2^32).
输出
the maximum amount of money.
样例输入
6 5 1 2 10 6 2
样例输出
17
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int Max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int n;
cin>>n;
int a[10001]={0},v[10001]={0};
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
v[1]=a[1];
for(int i=2;i<=n;i++)
{
v[i]=Max(v[i-1],v[i-2]+a[i]);
}
cout<<v[n]<<endl;
return 0;
}