#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
struct node
{
ll l = 0;
ll r = (1 << 30)-1;
ll sum = 0;
int cnt = 0;
node* left = NULL;
node* right = NULL;
void sons()
{
if(left == NULL)
{
left = new node;
left -> l = l;
left -> r = (l+r)/2;
right = new node;
right -> l = (l+r)/2+1;
right -> r = r;
}
}
void add(int poz, node* prev)
{
prev -> sons();
sum = prev->sum+poz;
cnt = prev->cnt+1;
if(l == r) return;
if(poz <= (l+r)/2)
{
right = prev -> right;
left = new node;
left -> l = l;
left -> r = (l+r)/2;
left -> add(poz,prev->left);
}
else
{
left = prev -> left;
right = new node;
right -> l = (l+r)/2+1;
right -> r = r;
right -> add(poz,prev->right);
}
}
pll get_kth(ll k, node* prev)
{
if(l == r)
{
return {min(k*l,sum-prev->sum),l};
}
sons();
prev -> sons();
if(right -> cnt - prev->right->cnt >= k)
{
return right -> get_kth(k,prev->right);
}
else
{
pll ans = left -> get_kth(k-(right->cnt-prev->right->cnt),prev->left);
return {ans.ff+right->sum-prev->right->sum,ans.ss};
}
}
};
const int tree_siz = 1024*512-1;
pll max_[tree_siz+1];
pll get_max(int akt, int p1, int p2, int s1, int s2)
{
if(p2 < s1 || p1 > s2) return {-1e18,-1};
if(p1 >= s1 && p2 <= s2) return max_[akt];
return max(get_max(akt*2,p1,(p1+p2)/2,s1,s2),get_max(akt*2+1,(p1+p2)/2+1,p2,s1,s2));
}
void upd(int v)
{
max_[v] = max(max_[v*2],max_[v*2+1]);
if(v != 1) upd(v/2);
}
void change_max(int ind, ll val)
{
max_[tree_siz/2+1+ind] = {val,ind};
upd((tree_siz/2+1+ind)/2);
}
vector<node> trees;
int n,k;
ll pref[250001];
pll get_seg(int l, int r)
{
if(r-l+1 < k) return {-1e18,1e18};
pll a = trees[r+1].get_kth(k,&trees[l]);
return {a.ff-(pref[r+1]-pref[l]),a.ss};
}
ll c[250001];
ll s[250001];
int opt_r[250001];
ll val[250001];
bool is_in[250001];
void eval_seg(int l, int r, ll ans)
{
pll val = get_seg(l,r);
if(val.ff != ans) return;
ll b = val.ss;
pll m = get_max(1,0,tree_siz/2,l,r);
while(m.ff >= b)
{
is_in[m.ss] = 1;
change_max(m.ss,-1e18);
m = get_max(1,0,tree_siz/2,l,r);
}
}
void solve(int l, int r, int l2, int r2)
{
if(l > r) return;
int mid = (l+r)/2;
pll best = {-1e18+1,-l2};
rep2(i,l2,r2)
{
best = max(best,{get_seg(mid,i).ff,-i});
}
best.ss *= -1;
opt_r[mid] = best.ss;
val[mid] = best.ff;
solve(l,mid-1,l2,best.ss);
solve(mid+1,r,best.ss,r2);
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//random_start();
cin >> n >> k;
rep(i,n) cin >> c[i];
rep(i,n) cin >> s[i];
rep(i,n) pref[i+1] = pref[i] + c[i];
rep(i,n) change_max(i,s[i]);
node beg;
trees.pb(beg);
rep(i,n)
{
node new_node;
new_node.add(s[i],&trees[i]);
trees.pb(new_node);
}
solve(0,n-k,0,n-1);
ll ans = -1e18;
rep(i,n-k+1)
{
ans = max(ans,val[i]);
}
vl ind;
rep(i,n) if(val[i] == ans) ind.pb(i);
ind.pb(n);
opt_r[n] = n-1;
rep(i,siz(ind)-1)
{
rep2(j,opt_r[ind[i]],opt_r[ind[i+1]])
{
eval_seg(ind[i],j,ans);
}
}
cout << ans << "\n";
rep(i,n) cout << is_in[i];
}
# | 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... |