W-unrequited LoveTime
limit:2000MS
Memory Limit:32768KB
64bit IO Format:%lld &%llu< /c7> SubmitStatusPracticeZOJ 3310Appoint Description:System Crawler (2015-04-16)
Description
Owen had been through unrequited love with N MM for a long time. In order to celebrate the 8th anniversary of ZOJ, he decided to "watch white".
One night, MMs is sitting in a ring. Owen's plan is for give them love letters secretly so the MMs who receive letters would know that Owen loves them. Besides, each MM have a "happy point". The greater it is, the happier Owen would be if that MM receives he love letter.
One more problem. MM is very sensitive, so she'll know if Owen (even secretly) drops a letter to the girl next to her. Meanwhile, MM is likely to be jealous. If She receives the love letter and finds this some other girl also gets a love letter, she'll Pia Owen to death.
Owen, as a smart boy, won ' t-let MM pia him, and decides to program to calculate the maximum sum of happy points he can get . However, love is blind. Poor Owen had lost the ability to program. So he turns.
Input
There is no more than test cases.
Each test case begins with an integer
N(1 <=
N<= 1000000).
Then followed
NIntegers (0<=
Ni<=), each representing a happy point for one MM. The first and last MM sit next to all other, forming a ring.
Each test case is occupies one line.
Output
One line for each test case.
An interger for each line indicating the maximum sun of happy points Owen can get.
Sample Input
5 1 2 3 4 56 1 2 3 4 5 6
Sample Output
812
#include <iostream> #include <cstdio> #include <string># include<cstring> #include <cmath> #include <algorithm> #include <cstdlib> #include <queue > #include <vector> #include <stack> #include <set>using namespace Std;int dp1[1000005];int dp2[ 1000005];int a[1000005];int N;int Main () {while (scanf ("%d", &n)!=eof) {int ans; for (int i=1;i<=n;i++) {dp1[i]=0; dp2[i]=0; } for (int i=1;i<=n;i++) scanf ("%d", &a[i]); DP1[1]=A[1]; for (int i=2;i<=n-1;i++) {dp1[i]=dp1[i-2]+a[i]; if (Dp1[i]<dp1[i-1]) dp1[i]=dp1[i-1]; } for (int i=2;i<=n;i++) {dp2[i]=dp2[i-2]+a[i]; if (Dp2[i]<dp2[i-1]) dp2[i]=dp2[i-1]; } ans=max (Dp1[n-1],dp2[n]); printf ("%d\n", ans); } return 0;}
(Ring DP) Zoj 3310