#include <bits/stdc++.h>
#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())
using namespace std;
template<typename U, typename V> bool maxi(U &a, V b) {
if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool mini(U &a, V b) {
if (a > b) { a = b; return 1; } return 0;
}
const int N = (int)250000 + 9;
const int mod = (int)1e9 + 7;
void prepare(); void main_code();
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
const bool MULTITEST = 0; prepare();
int num_test = 1; if (MULTITEST) cin >> num_test;
while (num_test--) { main_code(); }
}
void prepare() {};
int n, k, c[N];
long long s[N];
const long long INF = -(long long)1e15;
namespace DNC {
long long ans = INF;
int ar[N];
long long f[N], g[N];
void solve(int l, int r) {
if (r - l + 1 < k) return ;
int m = (l + r) >> 1;
int d = 0;
FOR(i, 1, k) f[i] = INF;
for(int i = m; i >= l; --i) {
int p = -1;
if (d == 0 or c[i] <= ar[d]) {
ar[++d] = c[i];
} else {
// c[i]>ar[d]
for(int j = 0; j < d; ++j) if (c[i] > ar[j + 1]) {
for(int k = d; k > j; --k)
ar[k+1] = ar[k];
ar[j+1] = c[i];
++d;
break ;
}
d = min(d, k);
}
long long sum = 0;
for(int j = 1; j <= d; ++j) {
sum += ar[j];
maxi(f[j], s[i-1] + sum);
}
}
FOR(i, 1, k) g[i] = INF;
d = 0;
for(int i = m + 1; i <= r; ++i) {
if (d == 0 or c[i] <= ar[d]) {
ar[++d] = c[i];
} else {
// c[i]>ar[d]
for(int j = 0; j < d; ++j) if (c[i] > ar[j + 1]) {
for(int k = d; k > j; --k)
ar[k+1] = ar[k];
ar[j+1] = c[i];
++d;
break ;
}
d = min(d, k);
}
long long sum = 0;
for(int j = 1; j <= d; ++j) {
sum += ar[j];
maxi(g[j], -s[i] + sum);
}
}
f[0] = s[m - 1];
g[0] = -s[m + 1];
// FOR(i, 0, k) cout << f[i] << ' '; cout << nl;
// FOR(i, 0, k) cout << g[i] << ' '; cout << nl;
// cout << nl;
for(int j = 0; j <= min(k, m - l + 1); ++j) {
maxi(ans, f[j] + g[k - j]);
}
solve(l, m);
solve(m + 1, r);
}
void sol() {
solve(1, n);
cout << ans << nl;
}
};
void main_code() {
cin >> n >> k;
FOR(i, 1, n) cin >> s[i], s[i] += s[i - 1];
FOR(i, 1, n) cin >> c[i];
if (k <= 200) {
DNC::sol();
return ;
}
}
/* Let the river flows naturally */
컴파일 시 표준 에러 (stderr) 메시지
trade.cpp: In function 'int main()':
trade.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
trade.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |