Codeforces 631E:Product Sum

来源:互联网 发布:怎么连接台湾的网络 编辑:程序博客网 时间:2024/06/06 00:46

E. Product Sum
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 that can be obtained by performing no more than one move.

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])i从1到n。然后你可以任意挪动一个数的位置,使得和最大,求和。

感觉是个三分的题,但是我没学过三分。。。

枚举每一个可能被挪动的元素,然后二分其前后位置,计算每一个位置产生的结果差值,求其最大。自己瞎搞水过的。。。

代码:

#pragma warning(disable:4996)#include <iostream>#include <functional>#include <algorithm>#include <cstring>#include <vector>#include <string>#include <cstdio>#include <cmath>#include <queue>#include <stack>#include <deque>#include <set>#include <map>using namespace std;typedef long long ll;#define INF 0x3fffffffffffffffconst ll mod = 1e9 + 7;const int maxn = 1e6 + 5;ll n, sum;ll val[maxn], pre[maxn];ll cal(ll pos, ll mid){ll res;if (pos > mid){res = (mid - pos)*val[pos] + (pre[pos - 1] - pre[mid - 1]);}else{res = (mid - pos)*val[pos] - (pre[mid] - pre[pos]);}return res;}void solve(){ll i, j, k;scanf("%I64d", &n);sum = 0;for (i = 1; i <= n; i++){scanf("%I64d", &val[i]);sum += i*val[i];pre[i] = val[i] + pre[i - 1];}ll le, ri, mid;ll ans = 0;for (i = 1; i <= n; i++){le = 1, ri = n;while (le < ri){mid = (le + ri) / 2;if (cal(i, mid) >= cal(i, mid + 1)){ri = mid;}else{le = mid + 1;}}ans = max(ans, cal(i, ri));}for (i = 1; i <= n; i++){le = 1, ri = n;while (le < ri){mid = (le + ri + 1) / 2;if (cal(i, mid) >= cal(i, mid - 1)){le = mid;}else{ri = mid - 1;}}ans = max(ans, cal(i, le));}printf("%I64d", ans + sum);}int main(){#ifndef ONLINE_JUDGE  freopen("i.txt", "r", stdin);freopen("o.txt", "w", stdout);#endif solve();return 0;}


0 0
原创粉丝点击