Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) B. Journey Planning

Tanya wants to go on a journey across the cities of Berland. There are nn cities situated along the main railroad line of Berland, and these cities are numbered from 11 to nn .

Tanya plans her journey as follows. First of all, she will choose some city c1c1 to start her journey. She will visit it, and after that go to some other city c2>c1c2>c1 , then to some other city c3>c2c3>c2 , and so on, until she chooses to end her journey in some city ck>ck1ck>ck−1 . So, the sequence of visited cities [c1,c2,,ck][c1,c2,…,ck] should be strictly increasing.

There are some additional constraints on the sequence of cities Tanya visits. Each city ii has a beauty value bibi associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities cici and ci+1ci+1 , the condition ci+1ci=bci+1bcici+1−ci=bci+1−bci must hold.

For example, if n=8n=8 and b=[3,4,4,6,6,7,8,9]b=[3,4,4,6,6,7,8,9] , there are several three possible ways to plan a journey:

  • c=[1,2,4]c=[1,2,4] ;
  • c=[3,5,6,8]c=[3,5,6,8] ;
  • c=[7]c=[7] (a journey consisting of one city is also valid).

There are some additional ways to plan a journey that are not listed above.

Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?

Input

The first line contains one integer nn (1n21051≤n≤2⋅105 ) — the number of cities in Berland.

The second line contains nn integers b1b1 , b2b2 , ..., bnbn (1bi41051≤bi≤4⋅105 ), where bibi is the beauty value of the ii -th city.

Output

Print one integer — the maximum beauty of a journey Tanya can choose.

Examples
Input
 
6
10 7 1 9 10 15
Output
 
26
Input
 
1
400000
Output
 
400000
Input
 
7
8 9 26 11 12 29 14
Output
 
55
大意是n个城市对应n个优美度,要求选定一条路线满足:城市序号升序,路线里相邻两个城市的序号差等于优美度的差,求路线优美度之和最大的一条路线。
可以看到第i个数减去i后的值一样的数对应的城市可以构成一条路线。这样直接开两个数组pos和neg,一个存非负的bi-i,一个存负的bi-i,扫一遍数组把值统计进去,再扫一遍pos和neg数组,找到最大的值输出即可。
#include <bits/stdc++.h>
using namespace std;
long long pos[400005]={0};
long long neg[400005]={0};
int main()
{
    int n;
    cin>>n;
    int i;
    long long temp;
    memset(pos,0,sizeof(pos));
    memset(neg,0,sizeof(neg));
    for(i=1;i<=n;i++)
    {
        scanf("%lld",&temp);    
        if(n==1)
        {
            cout<<temp;
            return 0;
        }
        if(temp-i>=0)
        {
            pos[temp-i]+=temp;
        }
        else 
        {
            neg[i-temp]+=temp;
        }
    }
    long long ans=0;
    for(i=0;i<=400002;i++)
    {
        long long mmax=neg[i]>pos[i]?neg[i]:pos[i];
        if(ans<mmax)ans=mmax;
    }
    cout<<ans;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lipoicyclic/p/12399238.html