You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.1 KiB
C++

#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 50;
using ll = long long;
ll d[N], ds[N];
template <typename T>
inline bool isMax(const T x) noexcept
{
return x == numeric_limits<decltype(x)>::max();
}
void pans(ll x)
{
printf("%lld", x);
exit(0);
}
int main()
{
ll H, n;
scanf("%lld%lld", &H, &n);
for (int i = 0; i < n; i++)
scanf("%lld", d + i);
ll sum = accumulate(d, d + n, 0);
ll ans = numeric_limits<ll>::max();
memcpy(ds, d, sizeof(ll) * n);
for (int i = 1; i < n; i++) ds[i] += ds[i - 1];
for (int i = 0; i < n; i++)
{
auto rest = H + ds[i];
if (rest <= 0)
pans(i + 1);
else
{
if (sum < 0)
{
//fprintf(stderr, "## %d %lld\n",i, (rest + -sum - 1) / -sum);
ans = min(ans, i + 1 + (rest + -sum - 1) / -sum * n);
}
}
}
if (isMax(ans)) ans = -1;
pans(ans);
return 0;
}