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>
using namespace std;
#define int long long
struct Line {
mutable int k, m, p;
bool operator<(const Line& o) const { return k < o.k; }
bool operator<(int x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<>> {
// for doubles, use inf = 1/.0, div(a,b) = a / b
static const int inf = 5e18;// implement this
long double div(int a, int b) { // floored division
return (long double)((long double)(a)/ (long double)(b)) ;
}
bool isect(iterator x, iterator y) {
if (y == end()) return x->p = inf, 0;
if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(int k, int m) { // insert y = kx + m
auto z = insert({k, m, 0}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
int query(int x) { // max query
assert(!empty());
auto l = *lower_bound(x);
return l.k * x + l.m;
}
};
// to use:
// LineContainer hull;
// hull.add(m, c);
// hull.query(x);
// to convert max queries to min queries:
// hull.add(-m, -c);
// -hull.query(x);
int dp[1000005], P[1000005];
priority_queue<int>pq;
int A[1000005];
main(){
ios::sync_with_stdio(0);cin.tie(0);
int n,k;cin >> n >> k;
for(int i=1;i<=n;i++){
cin >> A[i];
if(i > 1)pq.push(A[i] - A[i-1]);
}
pq.push(0);
while(pq.size() > n-k+1)pq.pop();
int ans = 0;
while(!pq.empty())ans += pq.top(), pq.pop();
cout << ans + k;
}
Compilation message (stderr)
stove.cpp:46:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
46 | main(){
| ^~~~
stove.cpp: In function 'int main()':
stove.cpp:54:18: warning: comparison of integer expressions of different signedness: 'std::priority_queue<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
54 | while(pq.size() > n-k+1)pq.pop();
| ~~~~~~~~~~^~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |