码迷,mamicode.com
首页 > 其他好文 > 详细

Journey Planning

时间:2020-03-07 16:19:18      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:clu   sso   理解   ecif   col   sequence   msu   return   list   

 

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
Copy
6
10 7 1 9 10 15
output
Copy
26
input
Copy
1
400000
output
Copy
400000
input
Copy
7
8 9 26 11 12 29 14
output
Copy
55
Note

The optimal journey plan in the first example is c=[2,4,5]c=[2,4,5].

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

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

 

主要是ci+1bci+1=cibci不好理解,如果看成 ibi,用哈希的办法就很好解决了。

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
//#include <xfunctional>
#define ll long long
#define mod 1000000007
using namespace std;
int dir[4][2] = { { 0,1 },{ 0,-1 },{ -1,0 },{ 1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;

int main()
{
    int n;
    cin >> n;
    vector<int> b(n + 1);
    for (int i = 1; i <= n; i++)
        cin >> b[i];
    map<int, vector<int>> mp;
    for (int i = 1; i <= n; i++)
    {
        if (mp.find(i - b[i]) == mp.end())
        {
            mp[i - b[i]] = vector<int>();
            mp[i - b[i]].push_back(b[i]);
        }
        else
        {
            mp[i - b[i]].push_back(b[i]);
        }
    }
    map<int, vector<int>>::iterator iter=mp.begin();
    ll sum = -INF;
    for (; iter != mp.end(); iter++)
    {
        ll t=0;
        for (int i = 0; i < iter->second.size(); i++)
        {
            t += iter->second[i];
        }
        sum = max(sum, t);
    }
    cout << sum;
    return 0;
}

 

 

Journey Planning

标签:clu   sso   理解   ecif   col   sequence   msu   return   list   

原文地址:https://www.cnblogs.com/dealer/p/12434563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!