#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <climits>
#include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define in insert
#define vll vector<ll>
#define endl "\n"
#define pll pair<ll,ll>
#define f first
#define s second
#define int ll
#define sz(x) (ll)x.size()
#define all(x) (x.begin(),x.end())
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> os;
const ll INF = 1e16;
const ll N =(1e5+5); // TODO : change value as per problem
const ll K = 105;
const ll MOD = 1e9+7;
template<typename T> struct rmq1 {
vector<T> v;
int n; static const int b = 64;
vector<uint64_t> mask;
vector<int> t;
int op(int x, int y) { return v[x] < v[y] ? x : y; }
int msb(uint64_t x) { return 63 - __builtin_clzll(x); }
int small(int r, int sz = b) { return r-msb(mask[r]<<(b-sz)>>(b-sz)); }
rmq1(const vector<T>& v_) : v(v_), n(v.size()), mask(n), t(n) {
for (uint64_t i = 0, at = 0; i < n; mask[i++] = at |= 1) {
at <<= 1;
while (at and op(i, i-msb(at&-at)) == i) at ^= at&-at;
}
for (int i = 0; i < n/b; i++) t[i] = small(b*i+b-1);
for (int j = 1; (1<<j) <= n>>6; j++) for (int i = 0; i+(1<<j) <= n>>6; i++)
t[(n>>6)*j+i] = op(t[(n>>6)*(j-1)+i], t[(n>>6)*(j-1)+i+(1<<(j-1))]);
}
T query(int l, int r) {
if (r-l+1 <= b) return v[small(r, r-l+1)];
int ans = op(small(l+b-1), small(r));
int x = (l>>6)+1, y = (r>>6)-1;
if (x <= y) {
int j = msb(y-x+1);
ans = op(ans, op(t[(n>>6)*j+x], t[(n>>6)*j+y-(1<<j)+1]));
}
return v[ans];
}
};
ll dp[N][K];
ll a[N];
int ng[N];
void solve(){
ll n;
cin >> n;
ll k;
cin >> k;
for(int i =1;i<=n;i++) cin >> a[i];
stack<int> st;
ng[1] = 1;
st.push(1);
for(int i =2;i<=n;i++){
while(!st.empty() and a[st.top()] <= a[i]) st.pop();
if(st.empty())ng[i]=1;
else ng[i]=st.top();
st.push(i);
}
int ma = 0;
vll v;
for(int i = 1;i<=n;i++){
ma = max(ma,a[i]);
dp[i][1] = ma;
v.pb(dp[i][1]);
}
rmq1<int> RMQ(v);
for(int i =2;i<=n;i++){
for(int j = 2;j<=k;j++) dp[i][j]=INF;
}
for(int j =2;j<=k;j++){
for(int i =1;i<=n;i++){
int l = ng[i];
int r = i-1;
if(l >= 1 and r <= n)
dp[i][j] = min(dp[i][j],a[i]+RMQ.query(l-1,r-1));
}
v.clear();
for(int i = 1;i<=n;i++) v.pb(dp[i][j]);
rmq1<int> RMQ2(v);
RMQ = RMQ2;
}
cout << dp[n][k] << endl;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
ll tt=1;
// cin >> tt;
while(tt--){
solve();
}
}
Compilation message
blocks.cpp: In instantiation of 'rmq1<T>::rmq1(const std::vector<_Tp>&) [with T = long long int]':
blocks.cpp:113:17: required from here
blocks.cpp:68:34: warning: comparison of integer expressions of different signedness: 'uint64_t' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
68 | for (uint64_t i = 0, at = 0; i < n; mask[i++] = at |= 1) {
| ~~^~~
blocks.cpp:70:39: warning: comparison of integer expressions of different signedness: 'long long int' and 'uint64_t' {aka 'long unsigned int'} [-Wsign-compare]
70 | while (at and op(i, i-msb(at&-at)) == i) at ^= at&-at;
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |