#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using ld = long double;
//#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
const int nmax = 25e4 + 5;
int K;
template<typename T>
struct Deq {
vector<T> v;
void push_front(T x) { v.emplace_back(x); }
T& operator[](const int& x) { return rbegin(v)[x]; }
void resize(int k) {
if(k >= sz(v)) return;
v.erase(begin(v), begin(v) + sz(v) - k);
return;
}
int size() { return sz(v); }
}; // trebuie facut persistent
struct Tree {
vector<int> g[nmax];
int p[nmax];
void add_edge(int a, int b) {
g[a].emplace_back(b);
p[b] = a;
}
struct mdeq : Deq<ll> {
ll lazy = 0;
};
mdeq dp[nmax];
vector<bool> merge[nmax];
int calculated[nmax];
Tree() {
memset(calculated, 0, sizeof(calculated));
}
template<class CB, class CB2> void calcdp(CB&& cost, CB2 atr, int node) {
if(calculated[node]) return;
calculated[node] = 1;
for(auto x : g[node]) {
calcdp(cost, atr, x);
dp[x].resize(K);
bool swapped = 0;
auto T = dp[x];
if(sz(T) > sz(dp[node]))
swap(T, dp[node]);
for(int i = 0; i < sz(T); i++)
dp[node][i] = max(T[i] + T.lazy - dp[node].lazy, dp[node][i]);
}
dp[node].push_front(cost(node) - dp[node].lazy);
dp[node].lazy += atr(node);
merge[node].resize(sz(dp[node]));
//cerr << node << ' ' << cost(node) << ' ' << atr(node) << ": ";
//for(int i = 0; i < sz(dp[node]); i++) cerr << dp[node][i] + dp[node].lazy << ' ';
//cerr << '\n';
return;
}
void propmerge(vector<int> order) {
for(auto node : order) {
for(auto x : g[node]) {
auto T = dp[x];
for(int i = 0; i < sz(T) && i + 1 < sz(dp[node]); i++) {
if(T[i] + T.lazy - dp[node].lazy == dp[node][i + 1])
merge[x][i] = merge[node][i + 1];
}
}
}
return;
}
};
ll spart[nmax];
signed main() {
cin.tie(0) -> sync_with_stdio(0);
int n;
cin >> n >> K;
for(int i = 1, x; i <= n; i++) {
cin >> spart[i];
spart[i] += spart[i - 1];
}
vector<int> v(n + 1);
for(int i = 1; i <= n; i++) cin >> v[i];
Tree toleft, toright;
vector<int> st;
for(int i = 1; i <= n; i++) {
while(sz(st) && v[st.back()] <= v[i]) st.pop_back();
if(sz(st) == 0) toleft.add_edge(0, i);
else toleft.add_edge(st.back(), i), toright.add_edge(i, st.back());
st.emplace_back(i);
}
st.clear();
for(int i = n; i > 0; i--) {
while(sz(st) && v[st.back()] < v[i]) st.pop_back();
if(sz(st) == 0) toright.add_edge(0, i);
else toright.add_edge(st.back(), i), toleft.add_edge(i, st.back());
st.emplace_back(i);
}
st.clear();
toleft.calcdp([&](int x) { return x == 0? 0 : -spart[x]; }, [&](int x) { return v[x]; }, 0);
toright.calcdp([&](int x) { return x == 0? 0 : spart[x - 1]; }, [&](int x) { return v[x]; }, 0);
ll best_cost = -1e18;
for(int P = 1; P <= n; P++) {
auto& X = toleft.dp[P];
auto& Y = toright.dp[P];
//cerr << sz(X) << ' ' << sz(Y) << '\n';
if(sz(X) > sz(Y)) swap(X, Y);
for(int i = 0; i < min(K, sz(X)); i++) {
int j = K - i - 1;
if(j >= sz(Y)) continue;
best_cost = max(best_cost, X[i] + Y[j] + X.lazy + Y.lazy - v[P]);
}
}
for(int P = 1; P <= n; P++) {
auto& X = toleft.dp[P];
auto& Y = toright.dp[P];
bool swapped = 0;
if(sz(X) > sz(Y)) swapped = 1, swap(X, Y);
for(int i = 0; i < min(K, sz(X)); i++) {
int j = K - i - 1;
if(j >= sz(Y)) continue;
if(best_cost == X[i] + Y[j] + X.lazy + Y.lazy - v[P]) {
if(!swapped)
toleft.merge[P][i] = 1, toright.merge[P][j] = 1;
else
toright.merge[P][i] = 1, toleft.merge[P][j] = 1;
}
}
}
vector<int> order(n);
iota(all(order), 1);
toleft.propmerge(order);
reverse(all(order));
toright.propmerge(order);
cout << best_cost << '\n';
for(int i = 1; i <= n; i++) {
cout << toleft.merge[i][0] || toright.merge[i][0];
}
cout << '\n';
}
/**
Töte es durch genaue Untersuchung\Töte es kann es nur noch schlimmer machen\Es lässt es irgendwie atmen
--
*/
Compilation message
trade.cpp: In function 'int main()':
trade.cpp:96:19: warning: unused variable 'x' [-Wunused-variable]
96 | for(int i = 1, x; i <= n; i++) {
| ^
trade.cpp: In instantiation of 'void Tree::calcdp(CB&&, CB2, int) [with CB = main()::<lambda(int)>; CB2 = main()::<lambda(int)>]':
trade.cpp:123:94: required from here
trade.cpp:57:15: warning: unused variable 'swapped' [-Wunused-variable]
57 | bool swapped = 0;
| ^~~~~~~
trade.cpp: In instantiation of 'void Tree::calcdp(CB&&, CB2, int) [with CB = main()::<lambda(int)>; CB2 = main()::<lambda(int)>]':
trade.cpp:124:98: required from here
trade.cpp:57:15: warning: unused variable 'swapped' [-Wunused-variable]
trade.cpp: In instantiation of 'void Tree::calcdp(CB&&, CB2, int) [with CB = main()::<lambda(int)>&; CB2 = main()::<lambda(int)>]':
trade.cpp:55:16: required from 'void Tree::calcdp(CB&&, CB2, int) [with CB = main()::<lambda(int)>; CB2 = main()::<lambda(int)>]'
trade.cpp:123:94: required from here
trade.cpp:57:15: warning: unused variable 'swapped' [-Wunused-variable]
trade.cpp: In instantiation of 'void Tree::calcdp(CB&&, CB2, int) [with CB = main()::<lambda(int)>&; CB2 = main()::<lambda(int)>]':
trade.cpp:55:16: required from 'void Tree::calcdp(CB&&, CB2, int) [with CB = main()::<lambda(int)>; CB2 = main()::<lambda(int)>]'
trade.cpp:124:98: required from here
trade.cpp:57:15: warning: unused variable 'swapped' [-Wunused-variable]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
29 ms |
51292 KB |
Partially correct |
2 |
Correct |
29 ms |
51292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
31 ms |
51292 KB |
Partially correct |
2 |
Correct |
28 ms |
51320 KB |
Output is correct |
3 |
Partially correct |
34 ms |
51232 KB |
Partially correct |
4 |
Partially correct |
30 ms |
51804 KB |
Partially correct |
5 |
Runtime error |
75 ms |
105300 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
31 ms |
51292 KB |
Partially correct |
2 |
Correct |
28 ms |
51320 KB |
Output is correct |
3 |
Partially correct |
34 ms |
51232 KB |
Partially correct |
4 |
Partially correct |
30 ms |
51804 KB |
Partially correct |
5 |
Runtime error |
75 ms |
105300 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
51292 KB |
Output is correct |
2 |
Partially correct |
187 ms |
148216 KB |
Partially correct |
3 |
Partially correct |
239 ms |
150080 KB |
Partially correct |
4 |
Partially correct |
204 ms |
150908 KB |
Partially correct |
5 |
Partially correct |
203 ms |
150400 KB |
Partially correct |
6 |
Partially correct |
200 ms |
150652 KB |
Partially correct |
7 |
Partially correct |
217 ms |
149628 KB |
Partially correct |
8 |
Partially correct |
226 ms |
150024 KB |
Partially correct |
9 |
Partially correct |
232 ms |
148504 KB |
Partially correct |
10 |
Partially correct |
201 ms |
149240 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
51292 KB |
Output is correct |
2 |
Partially correct |
187 ms |
148216 KB |
Partially correct |
3 |
Partially correct |
239 ms |
150080 KB |
Partially correct |
4 |
Partially correct |
204 ms |
150908 KB |
Partially correct |
5 |
Partially correct |
203 ms |
150400 KB |
Partially correct |
6 |
Partially correct |
200 ms |
150652 KB |
Partially correct |
7 |
Partially correct |
217 ms |
149628 KB |
Partially correct |
8 |
Partially correct |
226 ms |
150024 KB |
Partially correct |
9 |
Partially correct |
232 ms |
148504 KB |
Partially correct |
10 |
Partially correct |
201 ms |
149240 KB |
Partially correct |
11 |
Correct |
28 ms |
51280 KB |
Output is correct |
12 |
Partially correct |
186 ms |
148300 KB |
Partially correct |
13 |
Partially correct |
259 ms |
149888 KB |
Partially correct |
14 |
Partially correct |
213 ms |
150908 KB |
Partially correct |
15 |
Partially correct |
207 ms |
150400 KB |
Partially correct |
16 |
Partially correct |
203 ms |
150656 KB |
Partially correct |
17 |
Partially correct |
212 ms |
149720 KB |
Partially correct |
18 |
Partially correct |
250 ms |
149888 KB |
Partially correct |
19 |
Partially correct |
226 ms |
148368 KB |
Partially correct |
20 |
Partially correct |
200 ms |
149384 KB |
Partially correct |
21 |
Partially correct |
28 ms |
51288 KB |
Partially correct |
22 |
Partially correct |
30 ms |
51260 KB |
Partially correct |
23 |
Partially correct |
29 ms |
51804 KB |
Partially correct |
24 |
Runtime error |
73 ms |
105300 KB |
Execution killed with signal 11 |
25 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
29 ms |
51292 KB |
Partially correct |
2 |
Correct |
29 ms |
51292 KB |
Output is correct |
3 |
Partially correct |
31 ms |
51292 KB |
Partially correct |
4 |
Correct |
28 ms |
51320 KB |
Output is correct |
5 |
Partially correct |
34 ms |
51232 KB |
Partially correct |
6 |
Partially correct |
30 ms |
51804 KB |
Partially correct |
7 |
Runtime error |
75 ms |
105300 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |