7/10 luoguRound 10 庆典 div3T1

#include<bits/stdc++.h>
using namespace std;

int m,n,x;
int arr[100005];
int maxi = -1,maxn = -1;
int mini = -1,minn = INT_MAX;
long long cnt = 0,cnt_ = 0;

int main(){
	
	cin >> n >> m;
	for(int i = 0;i < n;i++){
		cin >> x;
		if(x > maxn){
			maxi = i;
			maxn = x;
		}
		if(x < minn){
			mini = i;
			minn = x;
		}
		
		cnt += x;
		//cout << "[Debug] cnt:" << cnt << endl;
		arr[i] = x;
	}
	//cout << "[Debug]" << mini << " " << minn << " " << maxi << " " << maxn << " " << cnt << endl;
	//get 数据最大值 最小值 数据总和 
	
	swap(arr[n-1],arr[mini]);
	if(maxi == n - 1){
		swap(maxi,mini);
	}
	mini = --n;
	//cout << "[Debug]" << mini << " " << minn << " " << maxi << " " << maxn << endl;
	//cout << "[Debug] n:" << n << endl << "[Debug]" << arr[0] << " " << arr[1] << " " << arr[2] << endl;
	//直接删除最小值
	
	for(int i = 0;i < n;i++){
		cnt_ += maxn - arr[i];
	}
	//cout << "[Debug] cnt_:" << cnt_ << " m+1:" << m + 1 << endl;
	
	if(cnt_ > m + 1){
		cout << cnt - arr[mini] - arr[maxi] + m << endl;
		return 0;
	} else {
		long long ans = cnt - minn + cnt_ - maxn;//n * maxn
		m -= cnt_;
		int k = m / n + 1;
		if(m%n == 0) k--;
		//cout << "[Debug] ans:" << ans << " m:" << m << " k:" << k << " n:" << n << endl;
		
		
		ans += m - k;
		cout << ans << endl;
		return 0;
	}
} 
posted @ 2021-07-10 16:11  Rosyr  阅读(25)  评论(0编辑  收藏  举报