Problem I: Inquiry I 1
I Inquiry I
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
(a1^2+ a2^2 +…+ ak^2)*(ak+1+…+an)
Input
• A single line containing an integer 2 ≤ n ≤ 106
.
• Then follow n lines, the ith of which contains the integer 1 ≤ ai ≤ 100.
Output
Output the maximal value of the given expression.
Sample Input 1 Sample Output 1
5
2
1
4
3
5
168
Sample Input 2 Sample Output 2
2
1
1
1
Sample Input 3 Sample Output 3
10
8
5
10
9
1
4
12
6
3
13
10530
这道题比赛的时候我也是想了老半天,很晚才想出来,我们可以先将除了第n个当作后面的,其他全是前面的,然后将前面的一个弄到后面,以此类推,找到最大值。
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
int n,s[maxn];
ll p,q,sum;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>s[i];
p+=s[i];
}
p-=s[1];q=s[1]*s[1];sum=p*q;
for(int i=2;i<n;i++){
p-=s[i];
q+=s[i]*s[i];
sum=max(sum,p*q);
}
printf("%lld\n",sum);
}