Music in Car
time limit per test

1 second

memory limit per test

256 megabytes

Sasha reaches the work by car. It takes exactly k minutes. On his way he listens to music. All songs in his playlist go one by one, after listening to the i-th song Sasha gets a pleasure which equals ai. The i-th song lasts for ti minutes.

Before the beginning of his way Sasha turns on some song x and then he listens to the songs one by one: at first, the song x, then the song (x + 1), then the song number (x + 2), and so on. He listens to songs until he reaches the work or until he listens to the last song in his playlist.

Sasha can listen to each song to the end or partly.

In the second case he listens to the song for integer number of minutes, at least half of the song's length. Formally, if the length of the song equals d minutes, Sasha listens to it for no less than  minutes, then he immediately switches it to the next song (if there is such). For example, if the length of the song which Sasha wants to partly listen to, equals 5 minutes, then he should listen to it for at least 3 minutes, if the length of the song equals 8 minutes, then he should listen to it for at least 4 minutes.

It takes no time to switch a song.

Sasha wants to listen partly no more than w songs. If the last listened song plays for less than half of its length, then Sasha doesn't get pleasure from it and that song is not included to the list of partly listened songs. It is not allowed to skip songs. A pleasure from a song does not depend on the listening mode, for the i-th song this value equals ai.

Help Sasha to choose such x and no more than w songs for partial listening to get the maximum pleasure. Write a program to find the maximum pleasure Sasha can get from the listening to the songs on his way to the work.

Input

The first line contains three integers nw and k (1 ≤ w ≤ n ≤ 2·105, 1 ≤ k ≤ 2·109) — the number of songs in the playlist, the number of songs Sasha can listen to partly and time in minutes which Sasha needs to reach work.

The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 104), where ai equals the pleasure Sasha gets after listening to thei-th song.

The third line contains n positive integers t1, t2, ..., tn (2 ≤ ti ≤ 104), where ti equals the length of the i-th song in minutes.

Output

Print the maximum pleasure Sasha can get after listening to the songs on the way to work.

Examples
input
7 2 11
3 4 3 5 1 4 6
7 7 3 6 5 3 9
output
12
input
8 4 20
5 6 4 3 7 5 4 1
10 12 5 12 14 8 5 8
output
19
input
1 1 5
6
9
output
6
input
1 1 3
4
7
output
0
Note

In the first example Sasha needs to start listening from the song number 2. He should listen to it partly (for 4 minutes), then listen to the song number 3 to the end (for 3 minutes) and then partly listen to the song number 4 (for 3 minutes). After listening to these songs Sasha will get pleasure which equals 4 + 3 + 5 = 12. Sasha will not have time to listen to the song number 5 because he will spend4 + 3 + 3 = 10 minutes listening to songs number 2, 3 and 4 and only 1 minute is left after that.

分析:two pointer+ two sets;

   一个set维护听part的歌曲,一个维护full;

   右指针向右时,优先考虑能否part,若不能,考虑full或将part里替换出一个来;

   左指针向右delete后,考虑能否将full的加到part里;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=2e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
inline void umax(int &p,int q){if(p<q)p=q;}
inline void umin(int &p,int q){if(p>q)p=q;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,w,a[maxn],t[maxn],ma,now;
set<pii>full,half;
int main()
{
int i,j;
scanf("%d%d%d",&n,&k,&w);
rep(i,,n)a[i]=read();
rep(i,,n)t[i]=read();
int l,r;
l=r=;
while(r<=n)
{
//right pointer;
while(r<=n)
{
if(k)
{
if(w>=(t[r]+)/)
{
w-=(t[r]+)/;
umax(ma,now+=a[r]);
half.insert(mp(t[r],r));
r++;
k--;
}
else break;
}
else
{
int tmp=half.begin()->fi;
if(tmp<=t[r]&&w>=(t[r]+)/-(tmp+)/+tmp)
{
w-=(t[r]+)/-(tmp+)/+tmp;
umax(ma,now+=a[r]);
auto p=half.begin();
full.insert(*p);
half.erase(p);
half.insert(mp(t[r],r));
r++;
}
else if(tmp>t[r]&&w>=t[r])
{
w-=t[r];
umax(ma,now+=a[r]);
full.insert(mp(t[r],r));
r++;
}
else break;
}
}
//left pointer;
if(l<r)
{
if(full.find(mp(t[l],l))!=full.end())
{
w+=t[l];
now-=a[l];
full.erase(mp(t[l],l));
}
else
{
w+=(t[l]+)/;
now-=a[l];
half.erase(mp(t[l],l));
k++;
if(!full.empty())
{
auto p=--full.end();
w+=p->fi-(p->fi+)/;
half.insert(*p);
k--;
full.erase(p);
}
}
l++;
}
else l++,r++;
}
printf("%d\n",ma);
return ;
}

随机推荐

  1. LeetCode 28 Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 思路:1.先将被除数和除数转化为long的非负 ...

  2. Spring:验证用户登录

    利用 Spring IOC 技术实现用户登录的验证机制,对用户进行登录验证. 首先利用 Spring 的自动装配模式将 User 对象注入到控制器中,然后将用户输入的用户名和密码与系统中限定的合法用户 ...

  3. Python 36 死锁现象和递归锁、信号量、Event事件、线程queue

    一:死锁现象和递归锁 所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远 ...

  4. iOS 点击事件传递及响应

    1.iOS中的事件 iOS中的事件可以分为3大类型: 触摸事件 加速计事件 远程控制事件这里我们只讨论iOS中的触摸事件. 1.1响应者对象(UIResponder) 在iOS中不是任何对象都能处理事 ...

  5. python 11:range(起始索引,终止索引,步数)(默认情况下步数为1,生成从起始索引,每次增加(终止索引-起始索引)/步数,到终止索引前的数字串)

    squares = [] for value in range(1,11): #第三参数默认为1,生成从1开始,每次增加1步数,到11前的10为止的数字串 square = value ** 2 sq ...

  6. TCP/IP详解(一)

    SYN中的MSS选项是告诉对端,本端在本地连接的每个TCP分节中愿意接收的最大数据量.发送端TCP使用接收端的MSS值作为发送分节的最大大小. TCP半关闭使用的情况较少,可用于通知对端本端数据已输入 ...

  7. 2-SAT的小总结(POJ 3683 POJ 3207)

    记住几个最重要的公式: xANDy=0<=>(x=>y′)AND(y=>x′) xANDy=1<=>(x′=>x)AND(y′=>y) xORy=0&l ...

  8. php解析 html类库 simple_html_dom

    如果从字符串加载html文档,需要先从网络上下载.建议使用cURL来抓取html文档并加载DOM中. 查找html元素 可以使用find函数来查找html文档中的元素.返回的结果是一个包含了对象的数组 ...

  9. HTTPS 为什么更安全,先看这些

    HTTPS 是建立在密码学基础之上的一种安全通信协议,严格来说是基于 HTTP 协议和 SSL/TLS 的组合.理解 HTTPS 之前有必要弄清楚一些密码学的相关基础概念,比如:明文.密文.密码.密钥 ...

  10. div自动适应浏览器窗口水平和垂直居中

    html <body> <div id="centerDiv">自动适应水平和垂直居中</div> </body> css ;;} ...