CodeForces 1141 E 模拟

http://codeforces.com/problemset/problem/1141/E

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

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

Output

9

Input

1000000000000 5
-1 0 0 0 0

Output

4999999999996

Input

10 4
-3 -6 5 4

Output

-1

题目大意: 给出怪物的初始血量和n次攻击每次对生命值造成的影响, 若能在有限次数内把怪物杀死则输出次数, 否则输出-1。

思路: 按照题意模拟就行了, 不过因为不在意细节错了很多次。细节真的很重要! 很容易想到用sum统计一轮攻击(n次)的影响,若在统计过程hp就小于0了, 那么记录位置,直接输出即可, 否则我们判断sum是否大于等于0, 若是则无解, 否则很容易想到设t=hp/sum, 然后继续往下做, 那么恭喜你, 成功掉入坑中。比如hp=10,两次攻击分别为-7 和 4,那么t=-3,这种方法就会得到错误的答案, 因此我们考虑在统计sum的过程中同时用MIN记录sum的最小值, 然后令hp+=MIN, 再做上述操作,然后遍历序列开始计算, 当hp<=0时,我们令hp-=MIN,再继续计算下去, 再次<=0时退出即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std;

typedef long long ll;

ll a[200005];

int main()
{
    int n;
    ll h;
    scanf("%lld %d",&h,&n);
    ll sum=0;
    ll MIN=0;
    int flag=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        sum+=a[i];
        MIN=min(MIN,sum);
        if(sum<=-h&&flag==0)
            flag=i;
    }
    if(flag)
        printf("%d\n",flag);
    else if(sum>=0)
        printf("-1\n");
    else
    {
        h+=MIN;
        int first=0;
        ll t=h/(-sum);
        h+=t*sum;
        ll temp=t*n;
        for(int i=1;i<=n;i++)
        {
            if(h<=0)
            {
                if(!first)
                    h-=MIN;
                else
                    break;
                first=1;
            }
            h+=a[i];
            ++temp;
            if(i==n)
                i=0;
        }
        printf("%lld\n",temp);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiji333/article/details/88784965