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 <iostream>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long int;

int main() {
    int S, T, D; cin >> S >> T >> D;
    vector<ll> W(D);

    const ll INF = 1LL << 60;
    ll sum = 0, mi = 0;
    for(int i=0; i<D; i++) cin >> W[i], sum += W[i], mi = min(mi, sum);

    if(S + mi > T and sum >= 0) {
        cout << -1 << endl;
    }
    else if(S + mi <= T) {
        ll ans = 0, weight = S;
        for(int i=0; i<2*D; weight += W[i%D], i++) {
            ll n_weight = weight + W[i%D];
            if(n_weight <= T) {
                ans += i + 1;
                break;
            }
        }
        cout << ans << endl;
    }
    else {
        ll dec = -sum;
        ll rem = S - T;
        ll loop = (rem + min<ll>(0, mi)) / dec;
        rem -= loop * dec;
        ll ans = loop * D;

        // fprintf(stderr, "dec = %lld, rem = %lld, ans = %lld\n", dec, rem, ans);
        ll weight = S - dec * loop;
        for(int i=0; i<2*D; weight += W[i%D], i++) {
            ll n_weight = weight + W[i%D];
            if(n_weight <= T) {
                ans += i + 1;
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Info{
	Info(ll arg_minus_value,ll arg_loc){
		minus_value = arg_minus_value;
		loc = arg_loc;
	}
	ll minus_value,loc;
};


int main(){

	ll S,T,D;
	scanf("%lld %lld %lld",&S,&T,&D);

	ll* table = new ll[D];
	for(int i = 0; i < D; i++)scanf("%lld",&table[i]);

	ll min_maximum = 0;

	vector<Info> V;
	if(table[0] < 0){
		V.push_back(Info(abs(table[0]),0));
		min_maximum = max(min_maximum,abs(table[0]));
	}

	for(int i = 1; i <= D-1; i++){
		table[i] += table[i-1];
		if(table[i] < 0){
			V.push_back(Info(abs(table[i]),i));
			min_maximum = max(min_maximum,abs(table[i]));
		}
	}

	ll tmp = S;

	for(int day = 1; day <= D; day++){
		tmp = S+table[day-1];
		if(tmp <= T){
			printf("%d\n",day);
			return 0;
		}
	}

	if(tmp >= S){
		printf("-1\n");
		return 0;
	}

	ll ans = D;

	while(tmp-min_maximum > T){
		ans += D;
		tmp -= V[V.size()-1].minus_value;
	}

	ll pre_loc = -1;

	for(int i = 0; i < V.size(); i++){

		if(V[i].loc <= pre_loc){
			ans += (V[i].loc+D)-pre_loc;
		}else{
			ans += V[i].loc-pre_loc;
		}
		if(tmp -V[i].minus_value<= T){
			printf("%lld\n",ans);
			return 0;
		}
		pre_loc = V[i].loc;
	}
	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;
    ll 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 = int64_t;
using P = pair<ll, ll>;
template <typename T> using V = vector<T>;

ll S, T, D;
V<ll> W;

ll solve() {
    cin >> S >> T >> D;
    W.resize(D);
    for(auto &e : W) cin >> e;
    V<ll> sum(D + 1);
    for(ll i = 0; i < D; i++) sum[i + 1] = sum[i] + W[i];
    auto min_v = min_element(sum.begin(), sum.end());
    if(*min_v == 0) return -1;
    if(S + *min_v > T && sum.back() >= 0) return -1;
    ll diff = T - S;
    ll init_ok = T - *min_v;
    ll x = max<ll>(0, S - init_ok);
    ll y = abs(sum.back());
    ll ret = 0;
    if(y) ret += x / y + !!(x % y);
    ll now = S - abs(sum.back()) * ret;
    ret *= D;
    for(ll i = 0; i < D; i++) {
        now += W[i];
        if(now <= T) return ret + i + 1;
    }
    assert(false);
}

int main() {
    cout << solve() << endl;
    return 0;
}

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

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
  ll s,t,d,w[100000],c=0,ans=1e10;
  cin>>s>>t>>d;
  for(int i=0;i<d;i++)cin>>w[i],c-=w[i];
  s-=t;
  for(ll i=0;i<d;i++){
    if(s<0)ans=min(ans,i);
    else if(c>0)ans=min(ans,i+s/c*d);
  }
  if(ans==1e10)ans=-2;
  cout<<ans+1<<endl;
  return 0;
}