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

E - Active Infants

时间:2020-04-25 16:58:58      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:int   names   cup   mat   als   copy   ssis   ppi   ons   

Time Limit: 2 sec / Memory Limit: 1024 MB

 

Score : 500 points

 

ps:一道dp,这次真的是我自己写的!一直没过,把int都改成ll过了= =没注意数据范围qaq

Problem Statement

There are NN children standing in a line from left to right. The activeness of the ii -th child from the left is Ai .

You can rearrange these children just one time in any order you like.

When a child who originally occupies the xx -th position from the left in the line moves to the y -th position from the left, that child earns Ax×|xy| happiness points.

Find the maximum total happiness points the children can earn.

Constraints

  • 2N2000
  • 1Ai109
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NA1 A2 ...... AN

Output

Print the maximum total happiness points the children can earn.


Sample Input 1 Copy

4
1 3 4 2

Sample Output 1 Copy

20

If we move the 1 -st child from the left to the 3 -rd position from the left, the 2 -nd child to the 4 -th position, the 3 -rd child to the 1 -st position, and the 4 -th child to the 2 -nd position, the children earns 1×|13|+3×|24|+4×|31|+2×|42|=20 happiness points in total.


Sample Input 2 Copy

Copy
6
5 5 6 1 1 1

Sample Output 2 Copy

Copy
58

Sample Input 3 Copy

Copy
6
8 6 9 1 2 1

Sample Output 3 Copy

Copy
85
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int mod=1e9+7;
const int N=2e3+5;
ll dp[N][N];
ll maxn;

struct nobe{
ll v,p;
}a[N];

bool cmp(nobe a,nobe b){
return a.v>b.v;
}

int main(){
ll n;
cin>>n;
for(ll i=0;i<n;i++){
    cin>>a[i].v;
    a[i].p=i+1;
}
sort(a,a+n,cmp);
for(ll i=0;i<=n;i++)
    for(ll j=0;j<=n;j++){
    if(i+j==n) {
        maxn=max(maxn,dp[i][j]);
        break;
        }
    int k=i+j;
    dp[i+1][j]=max(dp[i+1][j],dp[i][j]+abs(a[k].p-i-1)*a[k].v);
    dp[i][j+1]=max(dp[i][j+1],dp[i][j]+abs(a[k].p-n+j)*a[k].v);
}
cout<<maxn<<endl;
return 0;
}

  

E - Active Infants

标签:int   names   cup   mat   als   copy   ssis   ppi   ons   

原文地址:https://www.cnblogs.com/asunayi/p/12773702.html

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