Rotate It !!

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

standard input/output  Announcement

 
  • Statements

    Dzy and Fox have a sequence A consisting of N numbers [A1...AN]. Dzy starts by taking the first number, then Fox takes the second number, then Dzy takes the third number and so on, they continue taking turns until all of the N numbers are taken. The player with the highest sum of numbers wins.

    Since Dzy is your dear friend, you decided to rotate the sequence (you may rotate it as many times as you like) in order to maximize Dzy's sum of numbers.

    Rotation is defined as removing the first element from the beginning of the sequence and adding it to the end of the sequence.

    So given the sequence A , you have to help Dzy and let him achieve the maximum possible sum.

Input

The first line containts a single integer T, the number of test cases.

Then T testcases are given as follows :

The first line of each testcase contains a single integer N (1 ≤ n ≤ 104).

The second line of each testcase contains N space-separated integers [A1...AN],the elements of the sequence A (1 ≤ i ≤ n) ( - 109 ≤ Ai ≤ 109).

Output

Output T lines , The answer for each testcase which is the maximum achievable sum by Dzy if you help him.

Sample Input

Input
1 5 1 5 3 2 4
Output
12

Hint

Consider all 5 rotations of the sequence:

1 5 3 2 4 (Dzy score = 1 + 3 + 4 = 8)

5 3 2 4 1 (Dzy score = 8)

3 2 4 1 5 (Dzy score = 12)

2 4 1 5 3 (Dzy score = 6)

4 1 5 3 2 (Dzy score = 11)

题解:一个环状数据,Dzy从第一个开始取,每隔一个取一个,取的数最大是多少,注意若数为奇数,Dzy取的数一定最多;

想法是先找到总数,然后枚举每个位置为最后一个位置;

见代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN = 1e4 + ;
int num[MAXN];
typedef long long LL;
int main(){
int T, N;
scanf("%d", &T);
while(T--){
scanf("%d", &N);
LL sum = , cur;
if(N == ){
scanf("%lld", &cur);
printf("%lld\n", cur);
continue;
}
for(int i = ; i < N; i++){
scanf("%d", num + i);
sum += num[i];
}
if(N % == ){
cur = ;
for(int i = ; i < N; i+= )
cur += num[i];
printf("%lld\n", max(cur, sum - cur));
}
else{
LL sum1 = , sum2 = , cur1 = , cur2 = ;
for(int i = ; i < N; i++){
if(i % == )
sum1 += num[i];
else
sum2 += num[i];
}
LL ans = sum1;
for(int i = ; i < N; i++){
if(i % == )
cur1 += num[i];
else
cur2 += num[i];
if(i % == ){
ans = max(ans, sum2 - cur2 + cur1);
// printf("%lld\n", sum2 - cur2 + cur1);
}
else{
ans = max(ans, sum1 - cur1 + cur2);
// printf("%lld\n", sum1 - cur1 + cur2);
} }
printf("%lld\n", ans);
}
}
return ;
}

最新文章

  1. HDU--航海舰队
  2. 用PHP获取系统时间时,时间比当前时间少8个小时
  3. [Angularjs]视图和路由(三)
  4. bzoj1346: [Baltic2006]Coin
  5. jquery.tmpl 用法(附上详细案例)
  6. ASP.NET MVC学习系列 WebAPI初探
  7. rsync相关整理
  8. initEvent vs initMouseEvent
  9. 联系人数据库设计之ContactsTransaction
  10. 结构-行为-样式 - Angularjs 环境下Ztree结合JqueryUI实现拖拽
  11. 网络协议之ipv6
  12. 初读 c# IL中间语言
  13. 【Java】几道常见的秋招面试题
  14. .tar.xz文件的解压方法
  15. if 嵌套if 的先后顺序的区别 (自测)
  16. P2P原理及UDP穿透简单说明
  17. [IOI2007]Miners
  18. 5 CrawlSpider操作
  19. MapReduce的InputFormat过程的学习
  20. Java RSA公钥加密,私钥解密算法的尝试

热门文章

  1. 正则表达式获取URL参数
  2. Android ActionBar应用实战,高仿微信主界面的设计
  3. Android UI开发详解之ActionBar .
  4. gcc/g++/make 编译信息带颜色输出
  5. oracle SQL语句练习MERGE、模糊查询、排序、
  6. Linux查看网络即时网速
  7. css3标签
  8. (转) 值不能为空。参数名viewinfo(microsoft.sqlserver.management.sqlstudio.explorer)
  9. Jsp request
  10. mysqlbinlog详解