Coin-row problem

Coin-row problem
1000(ms)
65535(kb)
317 / 856
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<malloc.h>
#include<string.h>
#include<string>
#include<stack>
#include<math.h>
#include<algorithm>
using namespace std;
#define INF 99999999

int b[10005];

int main() {
	int n;
	cin>>n;
	int *a = new int [n+1];
	for(int i=1;i<=n;i++)
		cin>>a[i];
	//寻找方案
	int sum=0;
	b[0]=0,b[1]=a[1];
	for(int i=2;i<=n;i++){
		b[i]=b[i-2]+a[i]>a[i-1]?b[i-2]+a[i]:a[i-1];
	} 
	printf("%d\r\n",b[n]);
 	return 0;
}


阅读更多
换一批

没有更多推荐了,返回首页