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

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 n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.

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

There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value bi 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 ci and ci+1, the condition c i + 1 − c i = b c i + 1 − b c i c_{i+1}−c_i=b_{ci+1}−b_{ci} ci+1ci=bci+1bci must hold.

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

c=[1,2,4];
c=[3,5,6,8];
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 n (1≤n≤2e5) — the number of cities in Berland.

The second line contains n integers b1, b2, …, bn (1≤bi≤4e5), where bi is the beauty value of the i-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

赛时读题不认真,以为是最大上升子序列,后来发现很简单,就是把每个 b i − i b_i-i bii 对应的 a i a_i ai 记录下来,排序比大小即可,注意一定要用long long,我因为这个WA了两次,/(ㄒoㄒ)/~~,AC代码如下:

#include 
typedef long long ll;
using namespace std;
const int N=2e5+5;
ll n,a[N],b[N];
ll sum[N];
int main(){
    cin>>n;
    map<ll,ll>m;
    ll cnt=1;
    fill(sum,sum+N,0);
    for(ll i=1;i<=n;i++){
        scanf("%lld",&a[i]);
        b[i]=a[i]-i;
        if(m[b[i]]==0){
            m[b[i]]=cnt;
            sum[cnt]+=a[i];
            cnt++;
        }
        else sum[m[b[i]]]+=a[i];
    }
    sort(sum+1,sum+cnt);
    cout<<sum[cnt-1];
}

你可能感兴趣的:(Codeforces,排序,map)