Codeforces Round #547 (Div. 3) E

在这里插入图片描述
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;
#define ALL(obj) (obj).begin(), (obj).end()
#define REP(i, n) for(int i = 0;i < (n);++i)
#define endl '\n'
#define pb emplace_back
#define mkp make_pair
#define p_q priority_queue
#define MOD 1000000007LL
#define INF 2000000000
#define PI 3.1415926
// typedef long long ll;
#define int long long
signed main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int h, n, a[200000];
  int sum[200010]; sum[0] = 0; int MIN = 1145141919810931931LL;
  cin >> h >> n;
  REP(i, n){
    cin >> a[i];
    sum[i+1] = sum[i] + a[i];
    MIN = min(MIN, sum[i+1]);
    if(-sum[i+1] >= h){
      cout << i+1 << endl;
      return 0;
    }
  }
  if(sum[n] >= 0){
    cout << -1 << endl;
    return 0;
  }
  int tmp = h + MIN;
  int p = -sum[n];
  int ans = (tmp+p-1) / p * n;//减去最后一回合最大的伤害,补上p-1,否则少算一回合
  h -= p*((tmp+p-1) / p);
  REP(i, n+1){
    if(-sum[i] >= h){
      cout << ans+i << endl;
      return 0;
    }
  }

  return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43870114/article/details/88696160