题目
The Bureau for Artificial Problems in Competitions wants you to solve the following problem: Given n positive integers a1, . . . , an, what is the maximal value of
forma.jpg
Input:
• A single line containing an integer 2 ≤ n ≤ 10^6.
• Then follow n lines, the ith of which contains the integer 1 ≤ ai ≤ 100.
Output:
Output the maximal value of the given expression.
样例输入1
5
2
1
4
3
5
样例输出1
168
样例输入2
2
1
1
样例输出2
1
样例输入3
10
8
5
10
9
1
4
12
6
3
13
样例输出3
10530
题意:给出a[1]……a[n]的数组序列,求(a1a1+……akak)*(a(k+1)a(k+1)+……+anan)最大值。
思路:1s的时间限制,n为1e6暴力不可做,用前缀和。wa的原因是数组的数据类型为int,连续累加应l,r数组应为long long。
AC代码
#include <bits/stdc++.h>
using namespace std;
int a[1000010];
long long l[1000010],r[1000010];
int main()
{
int n;
scanf("%d",&n);
l[0]=0;
r[0]=0;
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
l[i]=l[i-1]+a[i]*a[i];
r[i]=r[i-1]+a[i];
}
long long ans=-1;
for(int i=1; i<=n; i++)
{
if((l[i]*(r[n]-r[i]))>ans)
{
ans=l[i]*(r[n]-r[i]);
}
}
printf("%lld\n",ans);
return 0;
}