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 int64_t
const int inf=1e18;
#ifdef LOCAL
#include "algo/debug.h"
#endif
#define f first
#define s second
template<class T> using V = vector<T>;
using vi = V<int>;
using vb = V<bool>;
using vs = V<string>;
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define len(x) (int)((x).size())
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define lb lower_bound
#define ub upper_bound
template<class T> int lwb(V<T>& a, const T& b) { return lb(all(a),b)-begin(a); }
template<class T> int upb(V<T>& a, const T& b) { return ub(all(a),b)-begin(a); }
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a=b, true : false; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a=b, true : false; }
#define pct __builtin_popcount
#define ctz __builtin_ctz
#define clz __builtin_clz
constexpr int p2(int x) { return (int)1 << x; }
constexpr int bits(int x) { return x == 0 ? 0 : 31-clz(x); } // floor(log2(x))
template<class T>void UNIQUE(V<T>& v) { sort(all(v)); v.erase(unique(all(v)),end(v)); }
template<class T,class U>void erase(T& t, const U& u) { auto it = t.find(u); assert(it != end(t)); t.erase(it); }
array<int,2> operator+(const array<int,2>& a,const array<int,2>&b){
return {a[0]+b[0],a[1]+b[1]};
}
int n;
vi a;
array<int,2> calc_dp(int lambda){
array<array<int,2>,2>dp;
dp[0]={0,0};
dp[1]={-inf,69};
for(int i=0;i<n;i++){
//continue the last segment
dp[1][0]+=a[i];
//start a new segment
ckmax(dp[1],dp[0]+array<int,2>{a[i]-lambda,1});
//already no segment
//end the segment
ckmax(dp[0],dp[1]);
}
auto score=max(dp[0],dp[1]);
return score;
}
void solve() {
int k;cin>>n>>k;
a.rsz(n);
for(int&i:a)cin>>i;
int lo=0,hi=inf;
while(hi-lo>1){
int lambda=(lo+hi)/2;
//regular dp
//dp(i,0)=score if we've considered first i elts assuming:
//0: we don't take the i-th elt
//1: we take the ith elt
//
//score is (sum,+groups)
calc_dp(lambda)[1]>=k?lo=lambda:hi=lambda;
}
cout<<calc_dp(lo)[0]+k*lo<<'\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
return 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |