This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fi first
#define se second
#define fin(s) freopen( s, "r", stdin );
#define fout(s) freopen( s, "w", stdout );
const long long N = 100100;
const long long Q = N * 30;
const long long mod = 998244353;
const long long MAGIC = 30;
using namespace std;
int n;
int k;
int a[N];
vector < int > v;
pair < long long, int > d[N];
pair < long long, int > get(long long f)
{
pair < long long, int > mn = {- a[1], 0};
for(int i = 1; i <= n; i++){
d[i] = {mn.fi + a[i] + f, mn.se + 1};
mn = min(mn, {d[i].fi - a[i + 1], d[i].se});
}
return d[n];
}
void solve()
{
cin >> n >> k;
long long ans = 0;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
sort(a + 1, a + n + 1);
long long l = 0, r = 1e12;
while(l < r){
long long m = (l + r) / 2;
if(get(m).se > k){
l = m + 1;
}
else{
r = m;
}
}
cout << get(l).fi - k * l + k << "\n";
}
bool mtest = false; int main()
{
//fin("input.txt");
//fout("output.txt");
//fin("island.in");
//fout("island.out");
ios_base::sync_with_stdio(0);
int TE = 1;
if(mtest)
cin >> TE;
while(TE--)
solve();
return 0;
}
Compilation message (stderr)
stove.cpp: In function 'void solve()':
stove.cpp:34:19: warning: unused variable 'ans' [-Wunused-variable]
long long ans = 0;
^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |