Time Limit: 2 second(s) | Memory Limit: 32 MB |
A soap company wants to advertise their product in a local area. In this area, there are n houses and the houses are placed in circular fashion, such that house 1 has two neighbors: house 2 and n. House 5has two neighbors: house 4 and 6. House n has two neighbors, house n-1 and 1.
Now the soap company has an estimation of the number of soaps they can sell on each house. But for their advertising policy, if they sell soaps to a house, they can't sell soaps to its two neighboring houses. No your task is to find the maximum number of estimated soaps they can sell in that area.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case starts with a line containing an integer n (2 ≤ n ≤ 1000). The next line contains n space separated integers, where the ith integer denotes the estimated number of soaps that can be sold to the ithhouse. Each of these integers will lie in the range [1, 1000].
Output
For each case, print the case number and the maximum number of estimated soaps that can be sold in that area.
Sample Input | Output for Sample Input |
3 2 10 100 3 10 2 11 4 8 9 2 8 | Case 1: 100 Case 2: 11 Case 3: 17 |
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
#define bug printf("hihi\n")
#define eps 1e-12
typedef long long ll;
using namespace std;
#define N 1005
int a[N];
int n;
int dp[2][N][2];
int main()
{
int i,j,t,ca=0;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
memset(dp,0,sizeof(dp));
dp[0][1][0]=0;
dp[1][1][1]=a[1];
for(int i=2;i<=n;i++)
{
dp[0][i][1]=a[i]+dp[0][i-1][0];
dp[0][i][0]=max(dp[0][i-1][1],dp[0][i-1][0]);
dp[1][i][1]=a[i]+dp[1][i-1][0];
dp[1][i][0]=max(dp[1][i-1][1],dp[1][i-1][0]);
}
dp[1][n][1]-=a[n];
int ans=0;
ans=max(dp[0][n][0],dp[0][n][1]);
ans=max(ans,dp[1][n][0]);
ans=max(ans,dp[1][n][1]);
printf("Case %d: %d\n",++ca,ans);
}
return 0;
}