Recall that the sequence b is a a subsequence of the sequence a if b can be derived from a by removing zero or more elements without changing the order of the remaining elements. For example, if a=[1,2,1,3,1,2,1], then possible subsequences are: [1,1,1,1], [3] and [1,2,1,3,1,2,1], but not [3,2,3] and [1,1,1,1,2].

You are given a sequence a consisting of n positive and negative elements (there is no zeros in the sequence).

Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.

In other words, if the maximum length of alternating subsequence is k then your task is to find the maximum sum of elements of some alternating subsequence of length k.

You have to answer t independent test cases.

Input

The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1≤n≤2⋅105) — the number of elements in a. The second line of the test case contains n integers a1,a2,…,an (−109≤ai≤109,ai≠0), where ai is the i-th element of a.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 (∑n≤2⋅105).

Output

For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of a.

Example

input

4

5

1 2 3 -1 -2

4

-1 -2 -1 -3

10

-2 8 3 8 -4 -15 5 -2 -3 1

6

1 -1000000000 1 -1000000000 1 -1000000000

output

2

-1

6

-2999999997

Note

In the first test case of the example, one of the possible answers is [1,2,3–,−1–––,−2].

In the second test case of the example, one of the possible answers is [−1,−2,−1–––,−3].

In the third test case of the example, one of the possible answers is [−2–––,8,3,8–,−4–––,−15,5–,−2–––,−3,1–].

In the fourth test case of the example, one of the possible answers is [1–,−1000000000,1,-1000000000,1,−1000000000].

题目大意:

找到最长的+ - + - + - + - + -(+ -交替即可)的序列,求最长序列的最大值。

这道题好像没什么好说的,就是找出每一段全正或全负的子序列最大值作为最长序列的数即可,可以用双指针算法。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll n, a[N];
int main() {
//freopen("in.txt", "r", stdin);
int t; cin >> t;
while (t--) {
cin >> n;
ll ans = 0, now = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
if (i == 1)
now = a[i];
else {
if (a[i] * a[i - 1] > 0)
now = max(now, a[i]);
else
ans += now, now = a[i];
}
}
ans += now;//别忘记加上最后一位
cout << ans << endl;
}
}

Codeforce1343C. Alternating Subsequence的更多相关文章

  1. [CC-MCO16306]Fluffy and Alternating Subsequence

    [CC-MCO16306]Fluffy and Alternating Subsequence 题目大意: 给定一个\(1\sim n(n\le3\times10^5)\)的排列\(a\). 对于一个 ...

  2. CF# 334 Alternative Thinking

    A. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  3. Codeforces Round #334 (Div. 2) C. Alternative Thinking 贪心

    C. Alternative Thinking Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/6 ...

  4. Alternative Thinking 找规律

    Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO ...

  5. CodeForces - 603A-Alternative Thinking (思维题)

    Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO ...

  6. 【33.10%】【codeforces 604C】Alternative Thinking

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. CF #636 (Div. 3) 对应题号CF1343

    unrated 选手悠闲做题,然后只做出四个滚蛋了 符合 div3 一贯风格,没啥难算法 E最后就要调出来了,但还是赛后才A的 CF1343A Candies 传送门 找到一个 \(x\),使得存在一 ...

  8. Codeforces Round #636 (Div. 3)

    比赛链接:https://codeforces.com/contest/1343 A - Candies 题意 有一数列 x + 2x + 4x + ... + 2k-1x = n,输出 k ≥ 2 ...

  9. codeforc 603-A. Alternative Thinking(规律)

    A. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes   Kevin ha ...

  10. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

随机推荐

  1. 2021牛客多校第一场 I题(DP)

    题意 给定一个长度为 \(n(n<=5000)\) 的排列,两个人轮流从这个序列中选择一个数,要求当前回合此人选择的数大于任意一个已经被选择的数,并且该数在数组中的位置 \(i\) 与此人上一次 ...

  2. 数据库系列:MySQL不同操作分别用什么锁?

    数据库系列:MySQL慢查询分析和性能优化 数据库系列:MySQL索引优化总结(综合版) 数据库系列:高并发下的数据字段变更 数据库系列:覆盖索引和规避回表 数据库系列:数据库高可用及无损扩容 数据库 ...

  3. 【解决方案】MySQL5.7 百万数据迁移到 ElasticSearch7.x 的思考

    目录 前言 一.一次性全量 二.定时任务增量 三.强一致性问题 四.canal 框架 4.1基本原理 4.2安装使用(重点) 版本说明 4.3引入依赖(测试) 4.4代码示例(测试) 五.文章小结 前 ...

  4. Educational Codeforces Round 26 Problem C

    C. Two Seals time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  5. 如何对U盘的使用权限进行管控

    对U盘的使用权限进行管控是保护企业信息安全的一项重要措施.以下是一些常见的方法,可帮助您有效管理和控制U盘的使用权限: 禁用U盘端口: 在公司计算机上禁用或限制USB端口的使用,特别是那些不需要使用U ...

  6. Kiractf

    信息收集 主机发现和端口扫描只开放了80的web服务 ​​ ‍ WEB打点 访问首页有文件上传,肯定可以利用一波.language那个页面甚至文件包含都写脸上了. root@Lockly tmp/ki ...

  7. Python——第二章:字符串操作——索引和切片

    索引: 按照位置提取元素 可以采用索引的方式来提取某一个字符(文字) s = "我叫周杰伦" print(s[3]) #程序员都是从0开始数,这里的3代表第4位,也就是" ...

  8. MyBatis 的缓存处理

    作为常见的 ORM 框架,在使用 MyBatis 的过程中可以针对相关的查询进行缓存处理以提高查询的性能.本文将简要介绍一下 MyBatis 中默认的一级缓存和二级缓存,以及自定义缓存的处理 MyBa ...

  9. 编写一个小而强大的 Windows 动态屏保壁纸

    写在前面 两年前我做了第一个开源软件 DreamScene2 动态桌面,如今受到了很多人的喜欢,这增加了我继续做好开源软件的信心.之前的这个软件一直有人希望我加入一个设置屏保壁纸的功能,因为 Drea ...

  10. 细说Python Lambda函数的用法,建议收藏!

    摘要:今天我就和大家聊聊lambda函数,在Python编程中,大家习惯将其称为表达式. 名称是用于引用或寻址任何实体的约定.我们周围的几乎所有事物都有名字.编程领域也与此一致.但这是必须命名的吗?还 ...