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. 旺店通·企业奇门和用友BIP接口打通对接实战

    旺店通·企业奇门和用友BIP接口打通对接实战 接通系统:旺店通·企业奇门 旺店通是北京掌上先机网络科技有限公司旗下品牌,国内的零售云服务提供商,基于云计算SaaS服务模式,以体系化解决方案,助力零售企 ...

  2. 【Javaweb】java中接口(interface)怎么用

    首先我们先了解什么是接口(interface) 实际情况中,又是我们必须从几个类中派生出一个子类,继承他们所有的属性和方法.但是,JAVA中是不支持多重继承的,那么为了满足这一目的,就有了接口,就可以 ...

  3. 【Android】做一个简单的每日打卡app-day01【还没做好】

    任务: 第一阶段目标: 1.用户注册:用户注册信息包括用户ID(学号).用户名(姓名),手机号码,用户单位(班级),用户班级四项基本信息,用户第一次注册后,用户姓名不用每次输入 . 2.每日总结打卡: ...

  4. [ABC327G] Many Good Tuple Problems

    题目链接 简化题意:有一个 \(n\) 个点的图,问有多少个长度为 \(M\) 的边序列,满足连边后图是二分图. \(n\le 30,m\le 10^9\) 考虑先强制要求无重边. 定义 \(f_{i ...

  5. Head First Java学习:第九章-构造器和垃圾收集器

    对象的前世今生 对象如何创建.存在何处以及如何让保存和抛弃更有效率. 会述及堆.栈.范围.构造器.超级构造器.空引用等. 1.内存的两个区域:堆和栈 堆(heap):对象的生存空间,又称为可垃圾回收的 ...

  6. 解决k8s删除pod后又重新创建了新的pod

    1.问题现象 2.原因 在Kubernetes中,当你删除一个Pod时,如果该Pod是由Deployment.ReplicaSet或PodController创建的,那么这个Pod会被标记为" ...

  7. 使用FRP实现内网穿透<阿里云服务器端+WINDOWS客户端>

    使用FRP实现内网穿透 1.准备条件 一个云服务器 一个FRP服务端文件,下载地址 一个FRP的windows客户端文件,下载地址 2.服务端 使用远程客户端工具,连接你自己的云服务器(我使用的是阿里 ...

  8. 安装华企盾DSC防泄密系统huawei Intel的电脑,加载驱动失败

    解决方法:从控制面板-[启用或关闭Windows功能]里面把[Hyper-V的功能]关闭 重启电脑再开启之后可以加密驱动则可以加载成功

  9. Vue学习笔记-快速入门

    整体代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  10. ElasticSearch之Delete index API

    删除指定的索引. 同时删除索引关联的数据.分片.元数据等相关的资源,因此执行前需要慎重. 命令样例如下: curl -X DELETE "https://localhost:9200/tes ...