Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	S, T, D := io.NextInt(), io.NextInt(), io.NextInt()
	w := make([]int, D)
	sum, min := 0, 0
	for i := 0; i < D; i++ {
		w[i] = io.NextInt()
		sum += w[i]
		min = intMin(sum, min)
		if S+sum <= T {
			io.Println(i + 1)
			return
		}
	}
	if sum >= 0 {
		io.Println(-1)
		return
	}
	ans := 0
	for ; S+min > T; ans += D {
		S += sum
	}
	for i := 0; i < D; i++ {
		S += w[i]
		if S <= T {
			io.Println(ans + i + 1)
			return
		}
	}
}

func intMin(a, b int) int {
	if a < b {
		return a
	}
	return b
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 100005;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int s,t,d;
    cin >> s >> t >> d;
    vector<int> vec(d);
    rep(i,d){
        cin >> vec[i];
    }
    vector<ll> sm(d);
    sm[0] = vec[0];
    ll mn = min(0LL,sm[0]);
    rep(i,d-1){
        sm[i+1] = sm[i] + vec[i+1];
        mn = min(sm[i+1],mn);
    }
    if(sm[d-1] < 0){
        if(mn <= t-s){
            ll nw = s;
            rep(i,d){
                nw += vec[i];
                if(nw <= t){
                    cout << i+1 << endl;
                    break;
                }
            }
        }else{
            ll diff = -sm[d-1];
            mn = -mn;
            ll sy = (s-t-mn-1)/diff + 1;
            ll nw = s-sy*diff;
            rep(i,d){
                nw += vec[i];
                if(nw <= t){
                    cout << sy*d + i+1 << endl;
                    break;
                }
            }
        }
    }else{
        if(mn <= t-s){
            ll nw = s;
            rep(i,d){
                nw += vec[i];
                if(nw <= t){
                    cout << i+1 << endl;
                    break;
                }
            }
        }else{
            cout << "-1\n";
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<P, ll> P3;
typedef pair<P ,P> PP;
constexpr ll MOD = ll(1e9) + 7;
constexpr int IINF = INT_MAX;
constexpr ll LLINF = LLONG_MAX;

int main() {
    ll s, t, d, x[100005]{}, xmin=LLINF;
    cin >> s >> t >> d;
    for(int i=0;i<d;i++){
        cin >> x[i+1];
        x[i+1] += x[i];
        if(xmin > x[i+1]){
            xmin = x[i+1];
        }
    }
    if(x[d] > 0 && s+xmin > t){
        cout << -1 << endl;
    }
    else{
        ll k = max(0LL, s+xmin-t)/abs(x[d]);
        ll ans = d*k;
        s += x[d]*k;
        for(int i=0;s>t;i=(i+1)%d,ans++){
            s += x[i+1]-x[i];
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;

int w[100001];
int main(){
    int s,t,d;
    cin>>s>>t>>d;
    ll sum=0;
    rep(i,d){
        cin>>w[i];
        sum+=w[i];
        if(-sum>=s-t){
            cout<<i+1<<endl;
            return 0;
        }
    }
    if(sum>=0){
        cout<<-1<<endl;
        return 0;
    }
    ll ans=0;
    int sub=s-t;
    ans+=sub/-sum*d;
    sub=sub%(-sum);
    rep(i,d){
        if(sub<=0){
            cout<<ans<<endl;
            return 0;
        }
        sub+=w[i];
        ++ans;
    }
    cout<<ans<<endl;
    return 0;
}

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

int main()
{
	int S, T, D;
	cin >> S >> T >> D;
	ll X = T - S;
	vector<ll> w(D);
	for (int i = 0; i < D; i++) {
		cin >> w[i];
	}
	ll sum = accumulate(w.begin(), w.end(), 0LL);
	ll res = LLONG_MAX;
	ll tmp = 0;
	for (int i = 0; i < D; i++) {
		tmp += w[i];
		if (tmp <= X) {
			res = min(res, i + 1LL);
		}
		else if (sum < 0) {
			res = min(res, (tmp - X + (-sum) - 1) / (-sum) * D + i + 1);
		}
	}
	cout << (res == LLONG_MAX ? -1 : res) << endl;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> mp;
ll inf =  1e9;


int main(){
    ll s,t,d;
    cin>>s>>t>>d;
    ll aim = s-t;
    vector<ll> w(d);
    for(ll i=0;i<d;i++)cin>>w[i];
    ll sumw = w[0];
    vector<ll> dp(d,0);
    dp[0] = w[0];
    for(ll i=1;i<d;i++)sumw += w[i],dp[i] = dp[i-1] + w[i];
    ll mi = inf;
    for(ll i=0;i<d;i++){
	if( aim + dp[i] <= 0 ){
	    cout<<i+1<<endl;
	    return 0;
	}
	mi =  min( mi , dp[i] );
    }
    if( sumw >= 0 ){
	cout<<-1<<endl;
	return 0;
    }
    mi = abs( mi );
    ll res = ( aim - mi )/(-sumw) ;
    aim += res *  sumw ;
    res *= d;
    while( aim  > 0 ){
	aim +=w[ res % d ];
	res++;
    }
    cout<<res<<endl;

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  long long s,t,d,w[100000],c=0;
  cin>>s>>t>>d;
  s-=t;
  for(int i=0;i<d;i++)cin>>w[i];
  while(0<s&&c<1e9)s+=w[c++%d];
  if(c==1e9)c=-1;
  cout<<c<<endl;
  return 0;
}

C++: #include <iostream>
using namespace	std;
int main(){

  int s,t,d,w[100000];
  cin>>s>>t>>d;
  for(int i=0,tmp;i<d;i++){
    cin>>tmp;
    if(i) w[i]=w[i-1]+tmp;
    else w[i]=tmp;
  }
  for(int i=0;i<d;i++){
    if(s+w[i]<=t){
      cout<<i+1<<endl;
      return 0;
    }
  }
  if(w[d-1]>=0){
    cout<<-1<<endl;
    return 0;
  }
  long long r=114514191911451419;
  for(int i=0;i<d;i++){
    r=min(r,(long long)(s-t+w[i]-w[d-1]-1)/-w[d-1]*d+i+1);
    cout<<r<<endl;
  }
  cout<<r<<endl;
  return 0;
}