#include <bits/stdc++.h>
#define ll long long
#define FOR(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
#define FOD(i,b,a) for (int i = (b), _a = (a); i >= _a; i--)
#define FORE(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(i) (1LL << (i))
#define CNTBIT(x) __builtin_popcount(x)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define left ___left
#define right ___right
#define pii pair<int, int>
#define DEBUG(n, a) FOR (i, 1, n) cout << a[i] << ' '; cout << endl;1234567890
#define lob lower_bound // >=
#define upb upper_bound // >
#define endl "\n"
using namespace std;
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y){ x = y; return true; } return false; }
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y){ x = y; return true; } return false; }
#define gogobruhbruh ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define file(name) if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
int MOD = 1e9 + 7;
int add(int &a, int b){ if ((a += b) >= MOD) a -= MOD; return a; }
int sub(int a, int b){ if ((a -= b) < 0) a += MOD; return a; }
int muti(int a, int b){ return (1LL * a * b) % MOD; }
int Pow(int x, int y){ int res = 1; for (; y; y >>= 1){ if (y & 1) res = muti(res, x); x = muti(x, x); } return res; }
const int MAX = 1e5 + 7;
const ll INF = 1e18 + 7;
int num, lim;
int val[MAX], L[MAX], R[MAX];
ll dp[MAX];
stack<int> st;
struct IT {
ll tree[MAX << 2];
void update(int id, int left, int right, int u, int v, ll val){
if (v < left || right < u)
return;
if (u <= left && right <= v){
minimize(tree[id], val);
return;
}
int mid = (left + right) >> 1;
update(id << 1, left, mid, u, v, val);
update(id << 1 | 1, mid + 1, right, u, v, val);
tree[id] = min(tree[id << 1], tree[id << 1 | 1]);
}
ll get(int id, int left, int right, int u, int v){
if (v < left || right < u)
return INF;
if (u <= left && right <= v)
return tree[id];
int mid = (left + right) >> 1;
return min(get(id << 1, left, mid, u, v),
get(id << 1 | 1, mid + 1, right, u, v));
}
} it;
void build(){
FOD (i, num, 1){
while (st.size() && val[st.top()] <= val[i])
st.pop();
if (st.empty())
R[i] = num;
else
R[i] = st.top() - 1;
st.push(i);
}
while (st.size())
st.pop();
FOR (i, 1, num){
while (st.size() && val[st.top()] <= val[i])
st.pop();
if (st.empty())
L[i] = 1;
else
L[i] = st.top() + 1;
st.push(i);
}
}
void solve(){
build();
memset(it.tree, 0x3f, sizeof(it.tree));
FOR (j, 2, lim){
FOR (i, 1, num){
it.update(1, 0, num, i, R[i], dp[i]);
dp[i] = it.get(1, 0, num, L[i] - 1, i - 1) + val[i];
// cout << j << ' ' << i << ' ' << dp[i] << endl;
}
}
cout << dp[num];
}
void read(){
cin >> num >> lim;
FOR (i, 1, num)
cin >> val[i],
dp[i] = max(dp[i - 1], 1LL * val[i]);
}
int main(){
gogobruhbruh;
file("main");
int test = 1;
// cin >> test;
while (test--)
read(),
solve();
}
Compilation message
blocks.cpp: In function 'int main()':
blocks.cpp:28:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | #define file(name) if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:117:5: note: in expansion of macro 'file'
117 | file("main");
| ^~~~
blocks.cpp:28:95: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | #define file(name) if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:117:5: note: in expansion of macro 'file'
117 | file("main");
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
1 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
1 ms |
3412 KB |
Output is correct |
6 |
Correct |
1 ms |
3412 KB |
Output is correct |
7 |
Correct |
1 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Incorrect |
1 ms |
3412 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
1 ms |
3412 KB |
Output is correct |
4 |
Correct |
1 ms |
3412 KB |
Output is correct |
5 |
Correct |
1 ms |
3412 KB |
Output is correct |
6 |
Correct |
1 ms |
3416 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
1 ms |
3412 KB |
Output is correct |
9 |
Incorrect |
1 ms |
3412 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
1 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
1 ms |
3412 KB |
Output is correct |
6 |
Correct |
1 ms |
3412 KB |
Output is correct |
7 |
Correct |
1 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Incorrect |
1 ms |
3412 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
1 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
1 ms |
3412 KB |
Output is correct |
6 |
Correct |
1 ms |
3412 KB |
Output is correct |
7 |
Correct |
1 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Incorrect |
1 ms |
3412 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |