题解:P14057 【MX-X21-T2】[IAMOI R5] 空气蛹
题解:P14057 【MX-X21-T2】[IAMOI R5] 空气蛹
题目传送门
题目大意:
有
注意:
当加上那个数后,原本的数如果超过了最大值,那么这个数将等于最大值。
就相当于以下操作:
思路:
考虑将一个杯子中的水倒出,变成空杯,就可以对序列进行从小到大排序了。排序后最需要进行倒走的一定是
代码:
#include<bits/stdc++.h>
using namespace std;
long long n,m;
long long a[100005];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
long long t;
cin>>t;
while(t--)
{
cin>>n>>m;
long long zhi=-1,u=0;
for(long long i=1;i<=n;i++)
{
cin>>a[i];
if(a[i]<a[i-1])
{
zhi=1;
}
u+=a[i];
}
if(zhi==-1)
{
cout<<u<<endl;
continue;
}
sort(a+1,a+1+n);
long long sum=a[1]+a[2]-m;
if(sum<=0)
{
// cout<<1;
cout<<u<<endl;
}
else
{
// cout<<1;
cout<<u-sum<<endl;
}
}
return 0;
}