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 <vector>
#include <algorithm>
using namespace std;
typedef long long int lli;

int main(){
	lli s,t,d;
	cin >> s >> t >> d;
	vector<lli> w(d);
	for(int i=0; i<d; i++){
		cin >> w[i];
	}

	lli sum = 0;
	lli minv=1e12;
	for(int i=0; i<d; i++){
		sum += w[i];
		if(sum < minv){
			minv = sum;
		}
	}
	if(sum >= 0 and s+minv > t){
		cout << -1 << endl;
		return 0;
	}

	lli rem = s-t;
	lli ans = max(rem +minv, 0LL) /(-sum);
	rem += ans*sum;
	ans *= d;
	for(int i=0; i<2*d; i++){
		ans++;
		rem += w[i%d];
		if(rem <= 0) break;
	}
	cout << ans << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
#ifdef DEBUG_ 
#include "../program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL<<60;

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

    ll sum = 0;
    REP(i, d) sum += w[i];

    auto check = [&](ll mid) {
        ll weight = s + (mid - 1) * sum; 
        REP(i, d) {
            weight += w[i];
            if(weight <= t) return true;
        }
        return false;
    };

    ll lb = 0, ub = 100100;
    while(ub-lb>1) {
        ll mid = (lb+ub)/2;
        if(check(mid)) ub = mid;
        else lb = mid;
    }

    {
        ll weight = s;
        REP(i, d) {
            weight += w[i];
            if(weight <= t) {
                cout << i+1 << endl;
                return 0;
            }
        }
    }

    if(ub == 100100) {
        cout << -1 << endl;
        return 0;
    }
    
    {
        ll weight = s + (ub - 1) * sum;
        REP(i, d) {
            weight += w[i];
            if(weight <= t) {
                cout << (ub-1)*d + i + 1 << endl;
                return 0;
            }
        }
    }

    return 0;
}

C++: #include<iostream>
#include<vector>

using namespace std;

#define int long long 

signed main(){

  int S, T, D;
  int now, minimum = 0;
  vector<int> w;
  
  cin>>S>>T>>D;

  w.resize(D);

  now = 0;
  
  for(int i = 0; i < D; i++){
    cin>>w[i];
    
    now += w[i];
    minimum = min(minimum, now);
    if(S + now <= T){
      cout<<i+1<<endl;
      return 0;
    }
  }

  if(now >= 0){
    cout<<-1<<endl;
  } else {
    int x = S - T + minimum;
    int y  = - (x - now -1) / now * now;
    int z = - (x - now -1) / now;

    
    for(int i = 0; i < D; i++){
      y += w[i];

      if(S + y <= T){
        cout<<i+1+z*D<<endl;
        return 0;
      }
    }
  }
  
  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;

ll w[100001];
int main(){
    ll 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)*(-sum);
    rep(i,d){
        if(sub<=0){
            cout<<ans<<endl;
            return 0;
        }
        sub+=w[i];
        ++ans;
    }
    cout<<ans<<endl;
    return 0;
}

C++: #include<iostream>
using namespace std;
int S,T,D;
long W[1<<17];
main()
{
	cin>>S>>T>>D;
	for(int i=0;i<D;i++)cin>>W[i];
	for(int i=1;i<D;i++)W[i]+=W[i-1];
	T-=S;
	long M=0;
	for(int i=0;i<D;i++)
	{
		if(T>=W[i])
		{
			cout<<i+1<<endl;
			return 0;
		}
		M=min(M,W[i]);
	}
	if(W[D-1]>=0)
	{
		cout<<-1<<endl;
		return 0;
	}
	long d=(M-T-W[D-1]-1)/-W[D-1];
	long ans=d*D;
	T-=W[D-1]*d;
	for(int i=0;i<D;i++)
	{
		if(T>=W[i])
		{
			cout<<ans+i+1<<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]);

	vector<Info> V;
	if(table[0] < 0){
		V.push_back(Info(abs(table[0]),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));
		}
	}

	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 pre_loc = -1;
	ll ans = D;

	while(true){

		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;
		}
		tmp -= V[V.size()-1].minus_value;
	}

	return 0;
}