The 2022 ICPC Asia Xian Regional Contest-J. Strange Sum

Given a sequence a1,a2,…,an.

You are going to select zero or more elements of aa so that: if you select aiai, then in any interval of length ii (formally, in a[j,j+i−1] for any 1≤j≤n−i+1) you can select at most 2 elements.

Calculate the maximal sum of the elements you select.

Input

The first line contains an integer nn (2≤n≤105).

The second line contains nn integers a1,a2,…,an (−10^9≤ai≤10^9).

Output

Output a single integer denoting the answer.

Examples

input

4
1 4 3 2

output

7

input

3
-10 -10 -10

output

0

解析:其实就是让你最多选两个数(可以不选),使得总和最大,1.如果有两个及以上的正数,选取最大的两个正数   2 .如果只有一个正数,就选它   3.如果没有,那么就是0

#include 
#include 
using namespace std;
typedef long long ll;
ll a[100005];//记录正数
int main()
{
	int n,m=0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		ll l;
		scanf("%lld",&l);
		if(l>0) a[m++]=l;
	}
	sort(a,a+m);
	if(m==0) printf("0\n");
	else if(m==1) printf("%lld\n",a[0]);
	else printf("%lld\n",a[m-1]+a[m-2]);
	return 0;
}

你可能感兴趣的:(c语言,c++,开发语言)