CodeForces - 855B ring 前缀和

邓布利多教授正在帮助哈利摧毁魂器。当他怀疑一个魂器出现在那里时,他去了冈特沙克。他看到Marvolo Gaunt的戒指,并将其确定为魂器。虽然他摧毁了它,但仍然受到诅咒的影响。斯内普教授正在帮助邓布利多解除诅咒。为此,他想给Dumbledore提供他制作的药水x滴。

x的值被计算为给定p,q,r和阵列a1,a2,......的p·ai + q·aj + r·ak的最大值,使得1≤i≤j≤k≤n。帮助Snape找到x的值。请注意x的值可能是负数。

Input

第一行输入包含4个整数n,p,q,r( - 1e9≤p,q,r≤1e9, 1≤n≤1e5)。

下一行输入包含n个空格分隔的整数a1,a2,... an( - 1e9≤ai≤1e9)。

Output

输出p≤ai+ q·aj + r·ak的最大值,只要1≤i≤j≤k≤n即可得到。

Examples

Input

5 1 2 31 2 3 4 5

Output

30

Input

5 1 2 -3-1 -2 -3 -4 -5

Output

12

Note

In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30.

In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.

题解,,前缀和,,用来保存偶从开始到当前状态的最大值,

后缀和,用来保存从当前位置到结尾处的最大值。

应题目要求,i<=j<=k,所以前缀数组保存前i项p*a[i]最大值..后缀保存后从第n项到第i项r*a[i]的最大值

然后对中间项进行枚举,,求出最大值

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
const int N=1E5+7;
const ll INF=-9223372036854775807+1;
ll arr[N];
ll before[N];
ll after[N];
int main(){
    int n;
    cin>>n;
    ll p,q,r;
    cin>>p>>q>>r;
    for(int i=1;i<=n;i++){
        scanf("%lld",&arr[i]);
    }
    for(int i=0;i<=n+1;i++){//数组的初始化,因为用到了最大值,所以要初始化为最小
        before[i]=after[i]=INF;
    }

    for(int i=1;i<=n;i++){
        before[i]=max(before[i-1],p*arr[i]);//保存前i项的最大值
    }

    for(int i=n;i>=1;i--){
        after[i]=max(after[i+1],r*arr[i]);//保存从第i项到第n项的最大值
    }

    ll ans=INF;
    for(int i=1;i<=n;i++){
        ans=max(ans,before[i]+q*arr[i]+after[i]);
    }
    printf("%lld\n",ans);

    return 0;
}

.

原文地址:https://www.cnblogs.com/Accepting/p/11373407.html

时间: 2024-11-05 20:30:31

CodeForces - 855B ring 前缀和的相关文章

Codeforces 855B - Marvolo Gaunt&#39;s Ring

855B - Marvolo Gaunt's Ring 思路:①枚举a[j],a[i]和a[k]分别用前缀最小值最大值和后缀最小值和后缀最大值确定. ②dp,dp[i][j]表示到第j为止,前i+1个值加起来的最大值. 代码: 代码①: #include<bits/stdc++.h> using namespace std; #define ll long long const int N=1e5+5; const int INF=0x3f3f3f3f; int a[N]; int premx

Codeforces 855B:Marvolo Gaunt&#39;s Ring(枚举,前后缀)

B. Marvolo Gaunt's Ring Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is sti

Codeforces 855B 简单DP

传送门:http://codeforces.com/problemset/problem/855/B emmmmmm--n比较打,1e15直接暴力的话会T,还有要注意的一点就是选取的ai,aj,ak,下标满足i<=j<=k,orz做的时候没注意WA了好多次orz 这里要用到的是简单的DP,循环扫2遍分别从头到尾,从尾到头记录至今为止的最大最小值,(因为要求pai+qaj+rak的最大值,所以只需要p,q,r>0时*最大值,<0时*最小值就好了,问题就在于如何处理下标不交叉的问题,d

CodeForces 460C——二分+前缀和—— Present

Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on his windowsill and star

Jzzhu and Numbers CodeForces - 449D (高维前缀和,容斥)

大意: 给定集合a, 求a的按位与和等于0的非空子集数. 首先由容斥可以得到 $ans = \sum \limits_{0\le x <2^{20}} (-1)^{\alpha} f_x$, 其中$\alpha$为$x$二进制中$1$的个数, $f_x$表示与和等于$x$的非空子集数. $f_x$是一个$20$维前缀和, 按传统容斥做法的话显然要超时, 可以每次求一维的和, 再累加 比方说对于2维前缀和, 用容斥的求法是这样 for (int i=1; i<=n; ++i) { for (in

Binary Numbers AND Sum CodeForces - 1066E (前缀和)

You are given two huge binary integer numbers aa and bb of lengths nn and mmrespectively. You will repeat the following process: if b>0b>0, then add to the answer the value a & ba & b and divide bb by 22 rounding down (i.e. remove the last d

Jury Meeting CodeForces - 854D (前缀和维护)

Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively num

Codeforces Round #427 (Div. 2) Problem C Star sky (Codeforces 835C) - 前缀和

The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 ≤ si ≤ c). Over time the stars twinkle. At moment 0 the i-th

Codeforces Gym 100637A A. Nano alarm-clocks 前缀和处理

A. Nano alarm-clocks Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/A Description An old watchmaker has n stopped nano alarm-clocks numbered with integers from 1 to n. Nano alarm-clocks count time in hours, and