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.

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=+;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int t,n,m,k,q;
unsigned long long ans;
ll cnt,flag,temp,sum;
ll h;
int a[N];
int main()
{
cin>>h>>n;
ll H=h;
ll minl=INF;
for(int i=;i<=n;i++){
cin>>a[i];
sum+=a[i];
minl=min(minl,sum);
if(h+sum<=){//如果h加上第一轮的sum小于0,就输出sum值所在下标
cout<<i<<endl;
return ;
}
}
if(minl>=||sum>=){// 和最小序列为正数或一轮所有值的和为正数,输出-1,代表不能实现将hp扣为0及一下
cout<<-<<endl;
return ;
}
cnt=(H+minl)/-sum;//hp加上和最小序列后除-sum得整数轮数,为n-2 或n-1 ,取决于数是到 和最小序列最后一个,还是之前就使hp<=0
if((H+minl)%-sum)//正好最大负值+整数个sum使hp=0
cnt++; //为n-1 或n
ans=(unsigned long long)cnt*n;//轮数乘以每轮个数
h=H+sum*cnt;
if(h<=){ //和最小序列和sum相等,即正好整数个sum使h<=0
cout<<ans<<endl;
return ;
}
for(int i=;i<=n;i++){//累加最后一轮
h+=a[i];
if(h<=){
cout<<ans+i<<endl;
return ;
}
}
}

题意分析:输入一个整数H,代表血量,每一轮的攻击消耗血量或增加血量,求消耗完时是第多少次攻击。

Codeforces1141E(E题)Superhero Battle的更多相关文章

  1. E. Superhero Battle Codeforces Round #547 (Div. 3) 思维题

    E. Superhero Battle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

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

    E. Superhero Battle A superhero fights with a monster. The battle consists of rounds, each of which ...

  3. CF1141E Superhero Battle

    A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly n minu ...

  4. E. Superhero Battle

    链接 [https://codeforces.com/contest/1141/problem/E] 题意 怪物开始的生命值,然后第i分钟生命值的变化 问什么时候怪物生命值为非正 分析 有一个巨大的坑 ...

  5. The 2015 China Collegiate Programming Contest -ccpc-c题-The Battle of Chibi(hdu5542)(树状数组,离散化)

    当时比赛时超时了,那时没学过树状数组,也不知道啥叫离散化(貌似好像现在也不懂).百度百科--离散化,把无限空间中无限的个体映射到有限的空间中去,以此提高算法的时空效率. 这道题是dp题,离散化和树状数 ...

  6. 【CF1141E】Superhero Battle

    \[x*p\ge y\rightarrow x=\lfloor{{y-1}\over p}\rfloor+1\]

  7. 【Codeforces 1141E】Superhero Battle

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 二分最后轮了几圈. 二分之后直接o(N)枚举具体要多少时间即可. 注意爆long long的情况. 可以用对数函数,算出来有多少个0 如果大于 ...

  8. 南阳ccpc C题 The Battle of Chibi && hdu5542 The Battle of Chibi (树状数组优化+dp)

    题意: 给你一个长度为n的数组,你需要从中找一个长度为m的严格上升子序列 问你最多能找到多少个 题解: 我们先对原序列从小到大排序,排序之后的序列就是一个上升序列 这里如果两个数相等的话,那么因为题目 ...

  9. 南阳ccpc C题 The Battle of Chibi 树状数组+dp

    题目: Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried ab ...

随机推荐

  1. 2019-2020-1 20199303《Linux内核原理与分析》第五周作业

    系统调用的三层机制 API:第一层是指Libc中定义的API,这些API封装了系统调用,使用int 0x80触发一个系统调用中断:当然,并非所有的API都使用了系统调用,如完成数学加减运算的API就没 ...

  2. Spring框架中文件目录遍历漏洞 Directory traversal in Spring framework

    官方给出的描述是Spring框架中报告了一个与静态资源处理相关的目录遍历漏洞.某些URL在使用前未正确加密,使得攻击者能够获取文件系统上的任何文件,这些文件也可用于运行SpringWeb应用程序的进程 ...

  3. Qt提示:setLayout: Attempting to set QLayout "" on MainWindow "MainWindow", which already has a layout

    如题,出现这个的原因是,如果你的窗口继承的是QMainwindow,需要设置setCentralWidget(); 如下: QWidget * widget = new QWidget ( mainW ...

  4. 鸟哥Linux私房菜(基础篇)——第五章:首次登入与在线求助 man page笔记

    1.X Winsows与文本模式的切换 ●[Ctrl] + [Alt] + [F1] ~ [F6] :文字接口登入 tty1 ~ tty6 终端机.        ●[Ctrl] + [Alt] + ...

  5. IDEA 之 Java项目复制

    1.复制一个项目,并改名字  2.更改以下文件名字  3.将以下文件中的原有名字,替换成更改后的名字(例如MyWebapp07替换成MyWebapp08) 4.将out文件夹给删除 5.然后用IDEA ...

  6. Android Resourse

    为什么80%的码农都做不了架构师?>>>   使用情景: 实现帧动画步骤的控制,这样动态的获取Drawable资源对应的R id,播放到那一步就加载到哪一步 private void ...

  7. 图论--拓扑排序--HDU-1285确定比赛名次

    Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...

  8. codeforce 272E Dima and Horses (假DFS)

    E. Dima and Horses Dima came to the horse land. There are n horses living in the land. Each horse in ...

  9. CF1336C Kaavi and Magic Spell

    CF1336C Kaavi and Magic Spell 区间dp 题意 给一个长度为 \(n\) 的字符串 \(S\) 和一个长度为 \(m\) 的字符串\(T\) ,\(1\le m\le n\ ...

  10. 03 Django下载和使用 三板斧httpresponse render redirect

    简介 是一个为完美主义者设计的web框架 The web framework for perfectionists with deadlines. Django可以使你能够用更少的代码,更加轻松且快速 ...