Codeforces Round #547 (Div. 3)E. Superhero Battle

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll N = 2e5 + 10;
const ll INF = 0x3f3f3f3f;
ll a[N];

int main()
{
	ll H,k,ans;
	scanf("%lld %lld",&H,&k);
	ll sum = 0;
	ll minn = INF;
	for (int i=0;i<k;i++)
	{
		scanf("%lld",&a[i]);
		sum += a[i];
		minn = min(sum,minn);//寻找在一个周期内减少的最大值
	}
	ll h = H;
	for (int i=0;i<k;i++)//判断第一个周期内能否解决战斗
	{
		h += a[i];
		if (h<=0)
		{
			ans = i + 1;
			printf("%lld\n",ans);
			return 0;
		}
	}
	if (sum>=0)
	{
		printf("-1\n");
		return 0;
	}
	h = H;
	ll m = (h + minn) / abs(sum);//minn和sum都是负的
	h += m * sum;
	ans = m * k;
	for (int i=0;;i++)
	{
		h += a[i%k];
		ans++;
		if (h<=0) break;
	}
	printf("%lld\n",ans);
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_40912854/article/details/89366538