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. 实现金蝶云星空与赛意SMOM系统的无缝数据对接

    1. 金蝶云星空:为运营协同与管控型企业提供通用ERP服务平台 金蝶云星空是基于当今先进管理理论和数十万家国内客户最佳应用实践开发的ERP服务平台.它针对事业部制.多地点.多工厂等企业和集团公司,提供 ...

  2. 外包杯学习进度(一) | 【Android】【Javaweb】Android与JavaWeb服务器交互教程——搭建环境

    前言 我们老师留了一个题目,这里就不写了,第一需要攻破的问题就是如何将app中的数据域javaweb进行传递,并可以回弹消息等问题.所以就开始了解一下这方面的信息. 资料积累 参考胡大炮的妖孽人生的博 ...

  3. 【Javaweb】implements Serializable是什么意思?反序列化是什么意思?

    为了保证数据传输的可靠 性,常常要implements Serializable,为什么? 对象本质上是虚无缥缈的,只是内存中的一个地址,如果想要让对象持久化,让对象在网络上传输,总不可能传送一个内存 ...

  4. 【Javaweb】Servlet* | 请求重定向【🖤🖤】

    请求重定向的含义 请求重定向,是指客户端给服务器发请求,然后服务器告诉客户端说.我给你一些地址,你去新地址访问,叫请求重定向(因为之前的地址可能已经废弃). 请求重定向的实现代码 请求重定向的第一种方 ...

  5. 2019牛客国庆集训派对day3 G排列(状压dp)

    题目传送门 一道很好的状压DP,状态是当前的占位情况,排序操作和第21次CSP认证的第四题作用类似. #include<cstdio> #include<vector> #in ...

  6. 使用Slurm集群进行分布式图计算:对Github网络影响力的系统分析

    本文分享自华为云社区<基于Slurm集群的分布式图计算应用实践:Github协作网络影响力分析>,作者:yd_263841138 . 1. 引言 Slurm(Simple Linux Ut ...

  7. 如何检测Windows服务停止后自动启动?自动运行.bat批处理文件?

    作者:西瓜程序猿 主页传送门:https://www.cnblogs.com/kimiliucn 前言 想要确保你的Windows服务即使在崩溃后也能自动重启吗?这篇文章教你如何用一个小巧的批处理脚本 ...

  8. 大语言模型底层架构丨带你认识Transformer

    本文分享自华为云社区<大语言模型底层架构你了解多少?大语言模型底层架构之一Transfomer的介绍和python代码实现>,作者: 码上开花_Lancer . 语言模型目标是建模自然语言 ...

  9. C++ Qt开发:Qt的安装与配置

    Qt是一种C++编程框架,用于构建图形用户界面(GUI)应用程序和嵌入式系统.Qt由Qt公司(前身为Nokia)开发,提供了一套跨平台的工具和类库,使开发者能够轻松地创建高效.美观.可扩展的应用程序. ...

  10. verdi的rc文件

    Verdi波形定位信号,将每次定位看的信号保存下来,存做rc文件,下次使用时直接打开就出来.不用再一个一个找信号. 原文 https://blog.csdn.net/sinat_43629962/ar ...