Codeforces Round #625 (Div. 2) B. Journey Planning /详解

B. Journey Planning
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 ci+1−ci=bci+1−bci 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≤2⋅105) — the number of cities in Berland.

The second line contains n integers b1, b2, …, bn (1≤bi≤4⋅105), 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
inputCopy
6
10 7 1 9 10 15
outputCopy
26
inputCopy
1
400000
outputCopy
400000
inputCopy
7
8 9 26 11 12 29 14
outputCopy
55
Note
The optimal journey plan in the first example is c=[2,4,5].

The optimal journey plan in the second example is c=[1].

The optimal journey plan in the third example is c=[3,6].

The meaning of problems:
Title obviously given conditions, must meet the selected sequence Ci + 1-Ci = Bci + 1-Bci, find the maximum value of beauty.

Ideas:

  1. Beautiful value i - j = index value beautiful i - j, it is clear that the difference value is equal to the beautiful subscripts difference.
  2. Term shift can be obtained by Ci + 1-Bci + 1 = Ci-Bci, Ci-Bi will meet a sequence that is selected corresponding to the number Ci contribute, directly at the time it entered a memory map trying to be difficult Finally, traversing map takes a maximum value.
  3. Note ci-bi also be negative, because last night, this is something that many people have been hack / fst.

Code:

#include<bits/stdc++.h>
#include<string>
#include<cstring>
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#define IOS ios::sync_with_stdio(false);cin.tie(0)
#define ll long long
//#define ll unsigned long long
#define inf 0x3f3f3f3f
#define mod 1000000007
#define eps 1e-6
#define PI acos(-1)
#define mysetit multiset<ll>::iterator
#define mymsetit multiset<ll>::iterator
#define mymapit map<ll,ll>::iterator
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define lowbit(x) (x&(-x))
#define mp make_pair
#define pb push_back
#define si size()
#define E exp(1.0)
#define fixed cout.setf(ios::fixed)
#define fixeds(x) setprecision(x)
using namespace std;
inline ll read(){ll s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;}
void put1(){ puts("Yes") ;}
void put2(){ puts("No") ;}
void put3(){ puts("-1"); }
ll qpf(ll a, ll b, ll p)
{ll ret = 0;while(b){if(b & 1) ret = (ret + a) % p;
a = (a + a) % p;b >>= 1;}return ret % p ;}
ll qp(ll a, ll n, ll p)
{ll ret = 1;while(n){if(n & 1) ret = qpf(ret, a, p);a = qpf(a, a, p);
n >>= 1;}return ret % p ;}
//θ=acos(L/2R);
 
const int manx=2e5+5;
 
map<ll,ll>mps;
int main(){
    ll n=read();
    for(int i=1;i<=n;i++){
        ll x=read();
        mps[x-i]+=x;
    }
    ll ans=0;
    for(auto &x: mps)
        ans=max(ans,x.se);
    printf("%lld\n",ans);
    return 0;
}
Published 75 original articles · won praise 40 · views 20000 +

Guess you like

Origin blog.csdn.net/JiangHxin/article/details/104605219