Codeforces Round #344 (Div. 2) E. Product Sum (三分)

网友投稿 283 2022-08-27

Codeforces Round #344 (Div. 2) E. Product Sum (三分)

E. Product Sum

time limit per test

memory limit per test

input

output

Blake is the boss of Kris, however, this doesn't spoil their friendship. They often gather at the bar to talk about intriguing problems about maximising some values. This time the problem is really special.

You are given an array a of length n. The characteristic of this array is the value

— the sum of the products of the valuesai by i. One may perform the following operation exactly once: pick some element of the array and move to any position. In particular, it's allowed to move the element to the beginning or to the end of the array. Also, it's allowed to put it back to the initial position. The goal is to get the array with the maximum possible value of characteristic.

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 200 000) — the size of the array a.

The second line contains n integers ai (1 ≤ i ≤ n, |ai| ≤ 1 000 000) — the elements of the array a.

Output

Print a single integer — the maximum possible value of characteristic of a

Examples

input

44 3 2 5

output

39

input

51 1 2 7 1

output

49

input

31 1 2

output

9

Note

In the first sample, one may pick the first element and place it before the third (before 5). Thus, the answer will be3·1 + 2·2 + 4·3 + 5·4 = 39.

In the second sample, one may pick the fifth element of the array and place it before the third. The answer will be1·1 + 1·2 + 1·3 + 2·4 + 7·5 = 49.

题解:给你出n个数,计算sum(i*a[i]) 。然后你可以任意挪动一个数的位置,且只能移动一个数。让你求移动后的最大值sum(i*a[i])。三分。枚举每个数,然后三分移动到各个位置寻求最大值。具体看代码吧。

AC代码:

#pragma comment(linker, "/STACK:102400000,102400000")//#include#include#include#include#include#include#include#include#include#include#include#include#include using namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair pii;typedef vector vi;#define mst(a) memset(a, 0, sizeof(a))#define M_P(x,y) make_pair(x,y)#define in freopen("in.txt","r",stdin) #define rep(i,j,k) for (int i = j; i <= k; i++) #define per(i,j,k) for (int i = j; i >= k; i--) #define lson l , mid , rt << 1 #define rson mid + 1 , r , rt << 1 | 1 const int lowbit(int x) { return x&-x; } const double eps = 1e-8; const int INF = 1e9+7; const ll inf =(1LL<<62) ;const int MOD = 1e9+7; const ll mod = (1LL<<32);const int N =2e5+7; const int M=100010;const ll MAX=1e18;//const int maxn=1001; template inline void getmax(T1 &a, T2 b) {if (b>a)a = b;} template inline void getmin(T1 &a, T2 b) {if (b>n; for(int i=1;i<=n;i++){ cin>>a[i]; sum[i]=sum[i-1]+a[i]; total += i*a[i]; } ll ans=-inf; for(int i=1;i<=n;i++) { Left=1; Right=n; cur=-inf; while(Left+1<=Right) { mid1=Left+(Right-Left)/3; mid2=Right-(Right-Left)/3; ans1=calc(i,mid1); ans2=calc(i,mid2); if(ans1>ans2) { Right=mid2-1; cur=max(cur,ans1); } else { Left=mid1+1; cur=max(cur,ans2); } } cur=max(cur,calc(i,Left)); if(cur>ans)ans=cur; } cout<

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Codeforces Round #333 (Div. 2) D. Lipshitz Sequence (单调栈)
下一篇:Codeforces #369 (Div. 2) E. ZS and The Birthday Paradox (勒让德定理+逆元)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~