Problem:
Slimming Plan
Chokudai loves eating so much. However, his doctor Akensho told him that he was overweight, so he finally decided to lose his weight.
Chokudai made a slimming plan of a $D$-day cycle. It is represented by $D$ integers $w_0, ..., w_{D-1}$. His weight is $S$ on the 0-th day of the plan and he aims to reduce it to $T$ ($S > T$). If his weight on the $i$-th day of the plan is $x$, it will be $x + w_{i\%D}$ on the $(i+1)$-th day. Note that $i\%D$ is the remainder obtained by dividing $i$ by $D$. If his weight successfully gets less than or equal to $T$, he will stop slimming immediately.
If his slimming plan takes too many days or even does not end forever, he should reconsider it.
Determine whether it ends or not, and report how many days it takes if it ends.
Input
The input consists of a single test case formatted as follows.
$S$ $T$ $D$
$w_0 ... w_{D-1}$
The first line consists of three integers $S$, $T$, $D$ ($1 \leq S, T, D \leq 100,000, S > T$). The second line consists of $D$ integers $w_0, ..., w_{D-1}$ ($-100,000 \leq w_i \leq 100,000$ for each $i$).
Output
If Chokudai's slimming plan ends on the $d$-th day, print $d$ in one line. If it never ends, print $-1$.
Sample Input 1
65 60 3
-2 3 -4
Output for Sample Input 1
4
Chokudai's weight will change as follows: $65 \rightarrow 63 \rightarrow 66 \rightarrow 62 \rightarrow 60$.
Sample Input 2
65 60 3
-2 10 -3
Output for Sample Input 2
-1
Chokudai's weight will change as follows: $65 \rightarrow 63 \rightarrow 73 \rightarrow 70 \rightarrow 68 \rightarrow 78 \rightarrow 75 \rightarrow ...$.
Sample Input 3
100000 1 1
-1
Output for Sample Input 3
99999
Sample Input 4
60 59 1
-123
Output for Sample Input 4
1

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
constexpr ll INF = 9009009009009009009LL;

int main(){
    ll s, t, d; cin >> s >> t >> d;
    vector<ll> w(d);
    rep(i, d) cin >> w[i];

    ll now = s;
    ll mndiff = INF;
    rep(i, d) {
        now += w[i % d];
        if (now <= t) {
            cout << i + 1 << endl;
            return 0;
        }
        mndiff = min(mndiff, now - s);
    }
    ll diff = now - s;

    if (now >= s) {
        cout << -1 << endl;
        return 0;
    }

    ll ans = 0;
    now = s;
    while (now + mndiff > t) {
        ans += d;
        now += diff;
    }

    rep(i, d) {
        now += w[i % d];
        if (now <= t) {
            cout << ans + i + 1 << endl;
            return 0;
        }
    }
    assert(false);
}
accuracy: 1
time_ms: 10
memory_kb: 3904
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;

/*
    考察
    モノイドを載せるダブリング上で二分探索
*/

pair<ll,ll> op(pair<ll,ll> l, pair<ll,ll> r){
	return {l.first+r.first, min(l.second,l.first+r.second)};
}

int main(){
	int S,T,D; cin>>S>>T>>D;
	vector<ll> W(D);
	for(int i=0; i<D; i++) cin>>W[i];

	int L=50;
	vector<vector<int>> nxt(L+1,vector<int>(D));
	vector<vector<pair<ll,ll>>> dat(L+1,vector<pair<ll,ll>>(D));
	for(int i=0; i<D; i++){
		nxt[0][i]=(i+1)%D;
		dat[0][i]={W[i],W[i]};
	}
	for(int i=1; i<=L; i++){
		for(int j=0; j<D; j++){
			nxt[i][j]=nxt[i-1][nxt[i-1][j]];
			dat[i][j]=op(dat[i-1][j],dat[i-1][nxt[i-1][j]]);
		}
	}

	ll lo=0,hi=1ll<<40;
	while(hi-lo>1){
		ll mid=(hi+lo)/2;

		ll k=mid;
		pair<ll,ll> res={0,1ll<<60};
		int now=0;

		for(int b=0; k>0; b++,k/=2){
			if(k&1){
				res=op(res,dat[b][now]);
				now=nxt[b][now];
			}
		}

		if(S+res.second<=T) hi=mid;
		else lo=mid;
	}

	if(hi==1ll<<40) hi=-1;
	cout<<hi<<endl;
}
accuracy: 1
time_ms: 50
memory_kb: 105300
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;

/*
    考察
    モノイドを載せるダブリング上で二分探索
*/

pair<ll,ll> op(pair<ll,ll> l, pair<ll,ll> r){
	return {l.first+r.first, min(l.second,l.first+r.second)};
}

int main(){
	int S,T,D; cin>>S>>T>>D;
	vector<ll> W(D);
	for(int i=0; i<D; i++) cin>>W[i];

	int L=62;
	vector<vector<int>> nxt(L+1,vector<int>(D));
	vector<vector<pair<ll,ll>>> dat(L+1,vector<pair<ll,ll>>(D));
	for(int i=0; i<D; i++){
		nxt[0][i]=(i+1)%D;
		dat[0][i]={W[i],W[i]};
	}
	for(int i=1; i<=L; i++){
		for(int j=0; j<D; j++){
			nxt[i][j]=nxt[i-1][nxt[i-1][j]];
			dat[i][j]=op(dat[i-1][j],dat[i-1][nxt[i-1][j]]);
		}
	}

	ll lo=0,hi=1ll<<60;
	while(hi-lo>1){
		ll mid=(hi+lo)/2;

		ll k=mid;
		pair<ll,ll> res={0,1ll<<60};
		int now=0;

		for(int b=0; k>0; b++,k/=2){
			if(k&1){
				res=op(res,dat[b][now]);
				now=nxt[b][now];
			}
		}

		if(S+res.second<=T) hi=mid;
		else lo=mid;
	}

	if(hi==1ll<<60) hi=-1;
	cout<<hi<<endl;
}
accuracy: 0.4473684210526316
time_ms: 60
memory_kb: 128784
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;

/*
    考察
    モノイドを載せるダブリング上で二分探索
*/

pair<ll,ll> op(pair<ll,ll> l, pair<ll,ll> r){
	return {l.first+r.first, min(l.second,l.first+r.second)};
}

int main(){
	int S,T,D; cin>>S>>T>>D;
	vector<ll> W(D);
	for(int i=0; i<D; i++) cin>>W[i];

	int L=40;
	vector<vector<int>> nxt(L+1,vector<int>(D));
	vector<vector<pair<ll,ll>>> dat(L+1,vector<pair<ll,ll>>(D));
	for(int i=0; i<D; i++){
		nxt[0][i]=(i+1)%D;
		dat[0][i]={W[i],W[i]};
	}
	for(int i=1; i<=L; i++){
		for(int j=0; j<D; j++){
			nxt[i][j]=nxt[i-1][nxt[i-1][j]];
			dat[i][j]=op(dat[i-1][j],dat[i-1][nxt[i-1][j]]);
		}
	}

	ll lo=0,hi=1<<30;
	while(hi-lo>1){
		ll mid=(hi+lo)/2;

		ll k=mid;
		pair<ll,ll> res={0,1ll<<60};
		int now=0;

		for(int b=0; k>0; b++,k/=2){
			if(k&1){
				res=op(res,dat[b][now]);
				now=nxt[b][now];
			}
		}

		if(S+res.second<=T) hi=mid;
		else lo=mid;
	}

	if(hi==1<<30) hi=-1;
	cout<<hi<<endl;
}
accuracy: 0.7105263157894737
time_ms: 40
memory_kb: 85576
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;

/*
    考察
    モノイドを載せるダブリング上で二分探索
*/

pair<ll,ll> op(pair<ll,ll> l, pair<ll,ll> r){
	return {l.first+r.first, min(l.second,l.first+r.first)};
}

int main(){
	int S,T,D; cin>>S>>T>>D;
	vector<ll> W(D);
	for(int i=0; i<D; i++) cin>>W[i];

	int L=40;
	vector<vector<int>> nxt(L+1,vector<int>(D));
	vector<vector<pair<ll,ll>>> dat(L+1,vector<pair<ll,ll>>(D));
	ll mn=0,sum=0;
	for(int i=0; i<D; i++){
		nxt[0][i]=(i+1)%D;
		sum+=W[i];
		mn=min(mn,sum);
		dat[0][i]={W[i],mn};
	}
	for(int i=1; i<=L; i++){
		for(int j=0; j<D; j++){
			nxt[i][j]=nxt[i-1][nxt[i-1][j]];
			dat[i][j]=op(dat[i-1][j],dat[i-1][nxt[i-1][j]]);
		}
	}

	ll lo=0,hi=1<<30;
	while(hi-lo>1){
		ll mid=(hi+lo)/2;

		ll k=mid;
		pair<ll,ll> res={0,1ll<<60};
		int now=0;

		for(int b=0; k>0; b++,k/=2){
			if(k&1){
				res=op(dat[b][now],res);
				now=nxt[b][now];
			}
		}

		if(S+res.second<=T) hi=mid;
		else lo=mid;
	}

	if(hi==1<<30) hi=-1;
	cout<<hi<<endl;
}
accuracy: 0.18421052631578946
time_ms: 30
memory_kb: 67940
final_rank: 20

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

int main() {
    ll S, T, D;
    cin >> S >> T >> D;
    vector<ll> W(D);
    rep(i,0,D) cin >> W[i];
    vector<ll> X(D+1,0);
    rep(i,0,D) X[i+1] = X[i] + W[i];
    if (X[D] >= 0) {
        rep(i,0,D) {
            S += W[i];
            if (S <= T) {
                cout << i+1 << endl;
                return 0;
            }
        }
        cout << -1 << endl;
        return 0;
    }
    ll MIN = inf;
    rep(i,0,D+1) chmin(MIN,X[i]);
    if (S+MIN<=T) {
        rep(i,0,D+1) {
            S += W[i%D];
            if (S <= T) {
                cout << i+1 << endl;
                return 0;
            }
        }
    }
    ll A = -X[D];
    ll Turn = (S-T+MIN)/A;
    ll ANS = Turn * D;
    S -= Turn * A;
    rep(i,0,D*2+1) {
        if (S <= T) {
            cout << ANS + i << endl;
            return 0;
        }
        S += W[i%D];
    }
}
accuracy: 1
time_ms: 10
memory_kb: 4748
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
using ll = long long;
template<class T> bool chmin(T &a,const T b){if(a>b){a=b;return 1;}return 0;}
template<class T> bool chmax(T &a,const T b){if(a<b){a=b;return 1;}return 0;}
const int INF = (1<<30)-1;
#define all(p) p.begin(),p.end()
const int mod=998244353;
using ld = long double;

int main(){
	ll S,T,D;
	cin>>S>>T>>D;
	S-=T;
	vector<ll> p(D);
	ll sum=0,val=0;
	rep(i,0,D) cin>>p[i],sum+=p[i],chmin(val,sum);	
	ll ans=0;
	if(sum<0){
		ll tmp=max(0ll,S+val-sum-1)/(-sum);
		ans+=tmp*D;
		S+=tmp*sum;
	}
	//cout<<ans<<" "<<S<<"\n";
	rep(i,0,D*3){
		if(S<=0){
			cout<<ans<<"\n";
			return 0;
		}
		ans++;
		S+=p[i%D];
	}
	cout<<"-1\n";
}
accuracy: 1
time_ms: 20
memory_kb: 3900
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
	int s, t, d;
	cin >> s >> t >> d;
	ll need = - (t - s), ans = 1ll << 60, h = -1;
	vector<ll> a(d), sumv(d + 1);
	for(int i = 0; i < d; i++){
		cin >> a[i];
		a[i] *= -1;
		sumv[i + 1] = sumv[i] + a[i];
		if(sumv[i + 1] >= need)ans = min(ans, (ll)i + 1);
		h = max(h, sumv[i + 1]);
	}
	if(ans >= (1ll << 60) && sumv[d] <= 0){
		cout << -1 << '\n';
		return 0;
	}
	if(ans < (1ll << 60)){
		cout << ans << '\n';
		return 0;
	}
	for(ll c = (need - h) / sumv[d]; c <= (need - h) / sumv[d] + 2; c++){
		for(int i = 0; i <= d; i++){
			if(c * sumv[d] + sumv[i] >= need)ans = min(ans, c * d + i);
		}
	}
	cout << ans << '\n';
}
accuracy: 1
time_ms: 20
memory_kb: 4708
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
	int s, t, d;
	cin >> s >> t >> d;
	ll need = - (t - s), ans = 1ll << 60, h = -1;
	vector<ll> a(d), sumv(d + 1);
	for(int i = 0; i < d; i++){
		cin >> a[i];
		a[i] *= -1;
		sumv[i + 1] = sumv[i] + a[i];
		if(sumv[i + 1] >= need)ans = min(ans, (ll)i + 1);
		h = max(h, sumv[i + 1]);
	}
	if(ans >= (1ll << 60) && sumv[d] <= 0){
		cout << -1 << '\n';
		return 0;
	}
	for(ll c = (need - h) / sumv[d]; c <= (need - h) / sumv[d] + 2; c++){
		for(int i = 0; i <= d; i++){
			if(c * sumv[d] + sumv[i] >= need)ans = min(ans, c * d + i);
		}
	}
	cout << ans << '\n';
}
accuracy: 0.25
time_ms: 10
memory_kb: 4248
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
	int s, t, d;
	cin >> s >> t >> d;
	ll need = - (t - s), ans = 1ll << 60, h = -1;
	vector<ll> a(d), sumv(d + 1);
	for(int i = 0; i < d; i++){
		cin >> a[i];
		a[i] *= -1;
		sumv[i + 1] = sumv[i] + a[i];
		if(sumv[i + 1] >= need)ans = min(ans, (ll)i + 1);
		h = max(h, sumv[i + 1]);
	}
	if(ans >= (1ll << 60) && sumv[d] <= 0){
		cout << -1 << '\n';
		return 0;
	}
	ll c = (need - h + sumv[d] - 1) / sumv[d], cnt = c * d, st = c * sumv[d];
	for(int i = 0; i <= d; i++, cnt++){
		if(st + sumv[i] >= need)ans = min(ans, cnt);
	}
	cout << ans << '\n';
}
accuracy: 0.25
time_ms: 10
memory_kb: 4260
final_rank: 16

code_snippet: #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <random>
#include <complex>
#include <bitset>
#include <iomanip>
#include <memory>
#include <functional>

#define rep(i, n, s) for (int i = (s); i < int(n); i++)
#define per(i, n, s) for (int i = (n) - 1; i >= int(s); i--)
#define MM << " " <<
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)

template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T>
using MaxHeap = std::priority_queue<T>;

using ll = long long;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;
using Pdd = std::pair<double, double>;

template <class T>
bool chmin(T &a, const T b) {
    if (b < a) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template <class T>
bool chmax(T &a, const T b) {
    if (a < b) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template <class T>
void vdeb(const std::vector<T> &da) {
    auto n = da.size();
    for (size_t i = 0; i < n; i++) {
        if (i == n - 1)
            std::cout << da[i];
        else
            std::cout << da[i] << " ";
    }
    std::cout << std::endl;
}
template<class T>
void vdeb(const std::vector<std::vector<T>> &da) {
    auto n = da.size();
    for (size_t i = 0; i < n; i++) {
        // std::cout << i << " : ";
        vdeb(da[i]);
    }
    std::cout << std::endl;
}

using namespace std;

int main() {
    ll s, t, d; cin >> s >> t >> d;
    vector<ll> w(d);
    rep(i,d,0) cin >> w[i];
    vector<ll> su(d+1);
    rep(i,d,0) su[i+1] = su[i] + w[i];
    ll k = s - t;
    ll m = -*min_element(all(su));
    if(m < k  && 0 <= su[d]) {
        cout << -1 << endl;
        return 0;
    }
    ll ans = 0;
    if(su[d]) ans = max(0LL, (k - m)) / (-su[d]);
    k += ans * su[d];
    // cerr << ans MM k MM m << endl;
    ans *= d;
    rep(i,d*2+1,0) {
        k += w[i%d];
        ++ans;
        if(k <= 0) break;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 4696
final_rank: 8

code_snippet: #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <random>
#include <complex>
#include <bitset>
#include <iomanip>
#include <memory>
#include <functional>

#define rep(i, n, s) for (int i = (s); i < int(n); i++)
#define per(i, n, s) for (int i = (n) - 1; i >= int(s); i--)
#define MM << " " <<
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)

template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T>
using MaxHeap = std::priority_queue<T>;

using ll = long long;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;
using Pdd = std::pair<double, double>;

template <class T>
bool chmin(T &a, const T b) {
    if (b < a) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template <class T>
bool chmax(T &a, const T b) {
    if (a < b) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template <class T>
void vdeb(const std::vector<T> &da) {
    auto n = da.size();
    for (size_t i = 0; i < n; i++) {
        if (i == n - 1)
            std::cout << da[i];
        else
            std::cout << da[i] << " ";
    }
    std::cout << std::endl;
}
template<class T>
void vdeb(const std::vector<std::vector<T>> &da) {
    auto n = da.size();
    for (size_t i = 0; i < n; i++) {
        // std::cout << i << " : ";
        vdeb(da[i]);
    }
    std::cout << std::endl;
}

using namespace std;

int main() {
    ll s, t, d; cin >> s >> t >> d;
    vector<ll> w(d);
    rep(i,d,0) cin >> w[i];
    vector<ll> su(d+1);
    rep(i,d,0) su[i+1] = su[i] + w[i];
    ll k = s - t;
    ll m = -*min_element(all(su));
    if(m < k  && 0 <= su[d]) {
        cout << -1 << endl;
        return 0;
    }
    ll ans = 0;
    if(su[d]) ans = (k - m) / (-su[d]);
    k += ans * su[d];
    // cerr << ans MM k MM m << endl;
    ans *= d;
    rep(i,d*2+1,0) {
        k += w[i%d];
        ++ans;
        if(k <= 0) break;
    }
    cout << ans << endl;
}
accuracy: 0.23684210526315788
time_ms: 10
memory_kb: 4396
final_rank: 19

code_snippet: #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <random>
#include <complex>
#include <bitset>
#include <iomanip>
#include <memory>
#include <functional>

#define rep(i, n, s) for (int i = (s); i < int(n); i++)
#define per(i, n, s) for (int i = (n) - 1; i >= int(s); i--)
#define MM << " " <<
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)

template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T>
using MaxHeap = std::priority_queue<T>;

using ll = long long;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;
using Pdd = std::pair<double, double>;

template <class T>
bool chmin(T &a, const T b) {
    if (b < a) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template <class T>
bool chmax(T &a, const T b) {
    if (a < b) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template <class T>
void vdeb(const std::vector<T> &da) {
    auto n = da.size();
    for (size_t i = 0; i < n; i++) {
        if (i == n - 1)
            std::cout << da[i];
        else
            std::cout << da[i] << " ";
    }
    std::cout << std::endl;
}
template<class T>
void vdeb(const std::vector<std::vector<T>> &da) {
    auto n = da.size();
    for (size_t i = 0; i < n; i++) {
        // std::cout << i << " : ";
        vdeb(da[i]);
    }
    std::cout << std::endl;
}

using namespace std;

int main() {
    ll s, t, d; cin >> s >> t >> d;
    vector<ll> w(d);
    rep(i,d,0) cin >> w[i];
    vector<ll> su(d+1);
    rep(i,d,0) su[i+1] = su[i] + w[i];
    ll k = s - t;
    ll m = -*min_element(all(su));
    if(m < k  && 0 < su[d]) {
        cout << -1 << endl;
        return 0;
    }
    ll ans = 0;
    if(su[d]) ans = (k - m ) / (-su[d]);
    else {
        if(m < k) {
            cout << -1 << endl;
            return 0;
        }
    }
    k += ans * su[d];
    // cerr << ans MM k MM m << endl;
    ans *= d;
    rep(i,d*2,0) {
        k += w[i%d];
        ++ans;
        if(k <= 0) break;
    }
    cout << ans << endl;
}
accuracy: 0.23684210526315788
time_ms: 10
memory_kb: 4368
final_rank: 18

code_snippet: #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <random>
#include <complex>
#include <bitset>
#include <iomanip>
#include <memory>
#include <functional>

#define rep(i, n, s) for (int i = (s); i < int(n); i++)
#define per(i, n, s) for (int i = (n) - 1; i >= int(s); i--)
#define MM << " " <<
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)

template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T>
using MaxHeap = std::priority_queue<T>;

using ll = long long;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;
using Pdd = std::pair<double, double>;

template <class T>
bool chmin(T &a, const T b) {
    if (b < a) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template <class T>
bool chmax(T &a, const T b) {
    if (a < b) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template <class T>
void vdeb(const std::vector<T> &da) {
    auto n = da.size();
    for (size_t i = 0; i < n; i++) {
        if (i == n - 1)
            std::cout << da[i];
        else
            std::cout << da[i] << " ";
    }
    std::cout << std::endl;
}
template<class T>
void vdeb(const std::vector<std::vector<T>> &da) {
    auto n = da.size();
    for (size_t i = 0; i < n; i++) {
        // std::cout << i << " : ";
        vdeb(da[i]);
    }
    std::cout << std::endl;
}

using namespace std;

int main() {
    ll s, t, d; cin >> s >> t >> d;
    vector<ll> w(d);
    rep(i,d,0) cin >> w[i];
    vector<ll> su(d+1);
    rep(i,d,0) su[i+1] = su[i] + w[i];
    ll k = s - t;
    ll m = -*min_element(all(su));
    if(m < k  && 0 < su[d]) {
        cout << -1 << endl;
        return 0;
    }
    ll ans = (k - m ) / (-su[d]);
    k += ans * su[d];
    // cerr << ans MM k MM m << endl;
    ans *= d;
    rep(i,d*2,0) {
        k += w[i%d];
        ++ans;
        if(k <= 0) break;
    }
    cout << ans << endl;
}
accuracy: 0.23684210526315788
time_ms: 10
memory_kb: 4360
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define inc(i, a, b) for (int i = (a); i <= (b); ++i)
#define dec(i, a, b) for (int i = (a); i >= (b); --i)

int main() {
    ll s, t, d;
    cin >> s >> t >> d;
    ll w[d + 1] = {};
    rep(i, d) cin >> w[i + 1], w[i + 1] += w[i];
    rep(i, d + 1) if (s + w[i] <= t) {
        cout << i << endl;
        return 0;
    }
    if (w[d] < 0) {
        ll ok = 100000000, ng = 0;
        while (ok - ng > 1) {
            ll mid = (ok + ng) / 2, f = 0;
            rep(i, d + 1) if (s + w[d] * mid + w[i] <= t) f = 1;
            (f ? ok : ng) = mid;
        }
        rep(i, d + 1) if (s + w[d] * ok + w[i] <= t) {
            cout << ok * d + i << endl;
            return 0;
        }
    } else {
        cout << -1 << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 4136
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ll S, T, D;
    cin >> S >> T >> D;
    S -= T;
    vector<ll> W(D);
    for (int i = 0; i < D; i++) {
        cin >> W[i];
        if (i)
            W[i] += W[i - 1];
    }
    ll ans = 1e12;
    for (int i = 0; i < D; i++) {
        if (W[D - 1] >= 0) {
            if (S + W[i] <= 0)
                ans = min(ans, i + 1ll);
        } else
            ans = min(ans, max((S + W[i] - W[D - 1] - 1), 0ll) / -W[D - 1] * D + i + 1);
    }
    if (ans == 1e12)
        ans = -1;
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3900
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ll S, T, D;
    cin >> S >> T >> D;
    S -= T;
    vector<ll> W(D);
    for (int i = 0; i < D; i++) {
        cin >> W[i];
        if (i)
            W[i] += W[i - 1];
    }
    ll ans = 1e12;
    for (int i = 0; i < D; i++) {
        if (W[D - 1] >= 0) {
            if (S + W[i] <= 0)
                ans = min(ans, i + 1ll);
        } else
            ans = min(ans, (S + W[i] - W[D - 1] - 1) / -W[D - 1] * D + i + 1);
    }
    if (ans == 1e12)
        ans = -1;
    cout << ans << endl;
}
accuracy: 0.25
time_ms: 10
memory_kb: 3644
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;

int main() {
    int S, T, D;
    cin >> S >> T >> D;
    vector<ll> w(D);
    for (int i = 0; i < D; i++) {
        cin >> w[i];
    }
    ll mi = w[0];
    for (int i = 0; i + 1 < D; i++) {
        w[i + 1] += w[i];
        mi = min(mi, w[i + 1]);
    }
    if (mi >= 0) {
        cout << -1 << endl;
        return 0;
    }
    mi *= -1;
    ll ans = 0;
    if (T + mi < S) {
        if (w[D - 1] >= 0) {
            cout << -1 << endl;
            return 0;
        }
        ll last = -w[D - 1];
        ll k = (S - (T + mi) + last - 1) / last;
        ans += k * D;
        S -= k * last;
    }
    for (int i = 0; i < D; i++) {
        if (S + w[i] <= T) {
            ans += i + 1;
            break;
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3864
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define REP(i,n) for(ll i=0;i<n;i++)
#define FOR(i,n1,n2) for(ll i=n1;i<n2;i++)
#define bFOR(i,n1,n2) for(ll i=n1;i>=n2;i--)
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
typedef long long int ll;
typedef unsigned long long int ull; 
typedef pair<ll,ll> Pi;
typedef tuple<ll,ll,ll> Tu;
const int INF=(ll)(1LL<<30)-1;
const double INFd=100000000000.0;
const double PI=3.14151926535;
const ll INFl=(ll)9223372036854775807;
const int MAX=10000;
const ll MOD=(ll)1e9+7;
const ll tMOD=(ll)998244353;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a, b)*b;}
int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0};
int mdx[8]={0,1,0,-1,1,1,-1,-1},mdy[8]={-1,0,1,0,1,-1,1,-1};


template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

ll s,t,d;
ll w[100100];
ll wsum[100100]={};
ll ans=1<<18;
int main(){
    cin>>s>>t>>d;
    REP(i,d){
        cin>>w[i];
        if(i==0){
            wsum[i]=w[i];
            if(s+wsum[i]<=t){
                cout<<i+1<<endl;
                return 0;
            }
        }else{
            wsum[i]=wsum[i-1]+w[i];
            if(s+wsum[i]<=t){
                cout<<i+1<<endl;
                return 0;
            }
        }
    }
    if(wsum[d-1]>=0){
        cout<<-1<<endl;
        return 0;
    }
    REP(i,d){
        ll ttt=((s-t+wsum[i]-wsum[i]-1)/(wsum[d-1]*-1))*d+i+1;
        ans=min(ans,ttt);
    }
    cout<<ans<<endl;
    return 0;

}
accuracy: 0.32894736842105265
time_ms: 10
memory_kb: 4832
final_rank: 13

code_snippet: #include <bits/stdc++.h>
#define inf (long long)(1e17)
using namespace std;

long long s, t, d;
vector<long long> w;

long long solve();

int main() {
  cin >> s >> t >> d;
  w.resize(d);
  for (auto &p : w) cin >> p;
  cout << solve() << endl;
  return 0;
}

long long solve() {
  long long res = inf, all;
  for (int i = 1; i < d; ++i) w[i] += w[i - 1];
  all = w[d - 1];
  for (int i = 0; i < d; ++i)
    if (s + w[i] <= t) return i + 1;
  if (all >= 0) return -1;
  all *= -1;
  for (int i = 0; i < d; ++i) {
    long long dif = s + w[i] - t;
    res = min(res, i + 1 + (dif + all - 1) / all * d);
  }
  if (res == inf) return -1;
  return res;
}
accuracy: 1
time_ms: 20
memory_kb: 3864
final_rank: 5