CF1763C
瞪眼,发现执行两次 后, 都变成 。于是当 中最大值的下标不为 和 时,设最大值下标为 ,则依次执行 ,不满足值域的就跳过。最终答案为 ,显然不会更大。
当 时,即使 中最大值的下标不为 和 ,我们也可以通过一系列操作转成上述情形:当 时,执行 即可转为 ;当 时,执行 即可转为 。
剩下 的情况显然可以特判,不再赘述。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=200005;
ll t,n,a[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>t;
while(t--)
{
ll d=0;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
d=max(d,a[i]);
}
if(n==1)
{
cout<<d<<"\n";
}
else if(n==2)
{
cout<<max(a[1]+a[2],2*abs(a[1]-a[2]))<<"\n";
}
else if(n==3)
{
if(d==a[1]||d==a[3]) cout<<3*d<<"\n";
else cout<<max(max(a[1]+a[2]+a[3],3*(d-min(a[1],a[3]))),3*max(a[1],a[3]))<<"\n";
}
else cout<<n*d<<"\n";
}
return 0;
}