Stressful Training

** Stressful Training**
Berland SU holds yet another training contest for its students today. n students came, each of them brought his laptop. However, it turned out that everyone has forgot their chargers!

Let students be numbered from 1 to n. Laptop of the i-th student has charge ai at the beginning of the contest and it uses bi of charge per minute (i.e. if the laptop has c charge at the beginning of some minute, it becomes c−bi charge at the beginning of the next minute). The whole contest lasts for k minutes.

Polycarp (the coach of Berland SU) decided to buy a single charger so that all the students would be able to successfully finish the contest. He buys the charger at the same moment the contest starts.

Polycarp can choose to buy the charger with any non-negative (zero or positive) integer power output. The power output is chosen before the purchase, it can’t be changed afterwards. Let the chosen power output be x. At the beginning of each minute (from the minute contest starts to the last minute of the contest) he can plug the charger into any of the student’s laptops and use it for some integer number of minutes. If the laptop is using bi charge per minute then it will become bi−x per minute while the charger is plugged in. Negative power usage rate means that the laptop’s charge is increasing. The charge of any laptop isn’t limited, it can become infinitely large. The charger can be plugged in no more than one laptop at the same time.

The student successfully finishes the contest if the charge of his laptop never is below zero at the beginning of some minute (from the minute contest starts to the last minute of the contest, zero charge is allowed). The charge of the laptop of the minute the contest ends doesn’t matter.

Help Polycarp to determine the minimal possible power output the charger should have so that all the students are able to successfully finish the contest. Also report if no such charger exists.

Input
The first line contains two integers n and k (1≤n≤2⋅105, 1≤k≤2⋅105) — the number of students (and laptops, correspondigly) and the duration of the contest in minutes.

The second line contains n integers a1,a2,…,an (1≤ai≤1012) — the initial charge of each student’s laptop.

The third line contains n integers b1,b2,…,bn (1≤bi≤107) — the power usage of each student’s laptop.

Output
Print a single non-negative integer — the minimal possible power output the charger should have so that all the students are able to successfully finish the contest.

If no such charger exists, print -1.

Examples
Input
2 4
3 2
4 2
Output
5
Input
1 5
4
2
Output
1
Input
1 6
4
2
Output
2
Input
2 2
2 10
3 15
Output
-1
Note
Let’s take a look at the state of laptops in the beginning of each minute on the first example with the charger of power 5:

charge: [3,2], plug the charger into laptop 1;
charge: [3−4+5,2−2]=[4,0], plug the charger into laptop 2;
charge: [4−4,0−2+5]=[0,3], plug the charger into laptop 1;
charge: [0−4+5,3−2]=[1,1].
The contest ends after the fourth minute.

However, let’s consider the charger of power 4:

charge: [3,2], plug the charger into laptop 1;
charge: [3−4+4,2−2]=[3,0], plug the charger into laptop 2;
charge: [3−4,0−2+4]=[−1,2], the first laptop has negative charge, thus, the first student doesn’t finish the contest.
In the fourth example no matter how powerful the charger is, one of the students won’t finish the contest.


题意:
每个学生的电脑有初始电量ai和每分钟耗电量bi。买了一个充电器,每分钟可以使电量增加x。求最小的x,使所有学生的电脑的电量在k分钟内都不为负。

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std ;

const int N = 2e5+100 ;
typedef long long ll ;
int b[N] ,d[N] ;
ll a[N] ,c[N] ;
int n , k ;

bool check(ll mid)
{
    memset(d , 0 ,sizeof(d)) ;
    int m = k ;
    for(int i = 1 ; i <= n ; i ++)
    {
        if(b[i]==0) continue ;
        ll t = a[i] ;
        while(t < c[i])
        {
            d[t/b[i]+1]++ ;// t/b[i]+1时间内必须充电一次
            t += mid ;
            if(m == 0)
                return false ;
            m-- ;
        }
    }
    for(int i = 1 ; i <= k ; i ++)// i时间内必须充电d[i]次
    {
        d[i] += d[i-1] ;
        if(d[i] > i)  return false ;
    }
    return true ;
}

int main()
{
    cin >> n >> k ;
    k-- ;
    for(int i = 1 ; i <= n ; i ++)
        cin >> a[i] ;
    for(int i = 1 ; i <= n ; i ++)
        cin >> b[i] , c[i] = ll(k)* b[i] ;

    ll l = 0 , r = 2e12 ;
    ll ans = -1 ;
    while(l <= r)
    {
        ll mid = l + r >> 1 ;
        if(check(mid))
            r = mid - 1 , ans = mid ;
        else
            l = mid + 1 ;
        //cout << l << ' ' << r << endl ;
    }
    cout << ans << endl ;
    return 0 ;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值