#include<bits/stdc++.h>
#define int long long
#define sd signed
#define db double
#define ld long db
#define il inline
using namespace std;
template<class T=sd>
il T read(){
	T f=1,x=0;
	char ch=getchar();
	while(ch<48||ch>57){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>47&&ch<58) x=(x<<3)+(x<<1)+ch-48,ch=getchar();
	return f*x;
}
template<class T>
il void write(T x){
	if(x<0){
		putchar('-');
		x=-x;
	}
	if(x>9) write((T)(x/10));
    putchar(x%10+48);
}
template<class T>il void writech(T x,char ch=' '){write(x),putchar(ch);}
template<class T>il void writeln(T x){write(x),putchar('\n');}
il char gtc(){return getchar();}
il void ptc(char ch){putchar(ch);}
il void ln(){putchar('\n');}
sd a[50010];
sd f(sd a,sd b){return a*b+1;}
sd main(){
	sd n=read(),maxn,minn;
	for(sd i=1;i<=n;i++) a[i]=read();
	maxn=a[1],minn=a[n];
	sort(a+1,a+n+1);
	for(sd i=2;i<=n;i++) maxn=f(maxn,a[i]);
	for(sd i=n-1;i>0;i--) minn=f(minn,a[i]);
	write(maxn-minn);
	return 0;
}

1 comments

  • @ 2024-3-9 11:06:30

    是每次在当前的所有数中选择最小的两个数进行操作,而不是从小到大排序后依次进行操作。因为你无法保证每次取出的都是当前最小的两个数。

    建议使用优先队列解决本题。

  • 1

Information

ID
6
Time
1000ms
Memory
256MiB
Difficulty
7
Tags
# Submissions
128
Accepted
32
Uploaded By