2026牛客寒假算法基础集训营1C题

链接

#include<vector>
#include<algorithm>

using namespace std;

void solve(){
    int n;
    cin >> n;
    vector<int>a(n);
    for(int i = 0; i < n; i ++)
        cin >> a[i];
    int l = a[0], r = a[n - 1];
    //一定要开long long
    long long max_b = 0;
    for(int i = 0; i < n; i ++){
        if (a[i] > max_b) max_b = a[i];
    }
    cout << (n - 2) * max_b + l + r << endl;
 }

int main(){
    int t;
    cin >> t;
    while(t--){
        solve();
    }
}

数组两端无法变化,根据题意其他的数组元素都可以变为数组的最大元素的值,求出数组中的最大值max,ans = (n - 2) * max + 数组左右两端的元素