题解:P17078 夏日甜点
Sunrise_up · · 题解
求点赞 qwq!
诈骗。
思路
首先变形
单独分析每一项,对于
- 若
a_p>a_i ,则a_p-|a_i-a_p|=a_i 。 - 若
a_p\le a_i ,则a_p-|a_i-a_p|=2a_p-a_i 。
因为有
所以
当取到
所以全部取
所以
所以输出
代码
时间复杂度
注意要开 long long。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+10;
int n,k,a[N],ans;
signed main(){
ios::sync_with_stdio(0);cin.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++)cin>>a[i],ans+=a[i];
cout<<ans;
}