E. Superhero Battle

5 篇文章 0 订阅
2 篇文章 0 订阅

A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly nn minutes. After a round ends, the next round starts immediately. This is repeated over and over again.

Each round has the same scenario. It is described by a sequence of nn numbers: d1,d2,…,dnd1,d2,…,dn (−106≤di≤106−106≤di≤106). The ii-th element means that monster's hp (hit points) changes by the value didi during the ii-th minute of each round. Formally, if before the ii-th minute of a round the monster's hp is hh, then after the ii-th minute it changes to h:=h+dih:=h+di.

The monster's initial hp is HH. It means that before the battle the monster has HH hit points. Print the first minute after which the monster dies. The monster dies if its hp is less than or equal to 00. Print -1 if the battle continues infinitely.

Input

The first line contains two integers HH and nn (1≤H≤10121≤H≤1012, 1≤n≤2⋅1051≤n≤2⋅105). The second line contains the sequence of integers d1,d2,…,dnd1,d2,…,dn (−106≤di≤106−106≤di≤106), where didi is the value to change monster's hp in the ii-th minute of a round.

Output

Print -1 if the superhero can't kill the monster and the battle will last infinitely. Otherwise, print the positive integer kk such that kk is the first minute after which the monster is dead.

Examples

input

Copy

1000 6
-100 -200 -300 125 77 -4

output

Copy

9

input

Copy

1000000000000 5
-1 0 0 0 0

output

Copy

4999999999996

input

Copy

10 4
-3 -6 5 4

output

Copy

-1

题意:给一个数n,一个数m,然后在给m个数。问n加这n个数最少多少次可以小于等于0.加完一轮循环加。

思路:求一个前缀和,记录一个最小前缀和然后二分答案判断,但当前缀和大于0的时候要特别判断。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long
#define INF 1e18
using namespace std;
const int maxn=2e5+100;
LL a[maxn],sum[maxn];
LL Min=INF,h;
int n;
bool check(LL x)
{
    if(h+(x-1)*sum[n]<=abs(Min))
    {
        return true;
    }
    else
    {
        return false;
    }
}
int main()
{
    scanf("%lld%d",&h,&n);

    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        sum[i]=sum[i-1]+a[i];
        Min=min(sum[i],Min);
    }
    LL v=h,flag=1,pos=1;
    if(sum[n]>=0)
    {
        for(int i=1;i<=n;i++)
        {
            v+=a[i];
           if(v<=0)
           {
               pos=i;
               break;
           }
        }
        if(v>0)
        {
            flag=0;
        }
        if(!flag)
        {
            printf("-1\n");
            return 0;
        }
        else
        {
           printf("%d\n",pos);
           return 0;
        }
    }
    LL l=0,r=h/(sum[n]*(-1))+2;
    //cout<<l<<" "<<r<<endl;
    while((r-l)>1)
    {
        LL mid=(l+r)/2;
        //cout<<l<<" "<<r<<endl;
        if(check(mid))
        {
            r=mid;
        }
        else
        {
            l=mid;
        }
    }
    LL ans=(r-1)*n;
    v=h+(r-1)*sum[n];
    for(int i=1;i<=n;i++)
    {
        v+=a[i];
        ans++;
        if(v<=0)
        {
            break;
        }
    }
    printf("%lld\n",ans);
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值