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 <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <limits>
#include <cmath>
#include <cassert>

using namespace std;
using ll = long long;

const int INF = 1<<30;
const int MOD = (ll)1e9 + 7;
const int MAX_N = (ll)1e5 + 5;
#define debug(x) cout << #x << ": " << x << endl

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll S, T, D; cin >> S >> T >> D;
    vector<ll> w(D);
    ll cycle = 0;
    for(ll i = 0; i < D; i++)
    {
        cin >> w[i];
        cycle += w[i];
    }
    bool flag = false;
    ll tmp = S - T;
    vector<ll> sum(D + 1, 0LL);
    vector<pair<ll, ll>> v;
    v.push_back(make_pair(0LL, 0LL));
    for(ll i = 0; i < D; i++)
    {
        tmp += w[i];
        sum[i + 1] += sum[i] + w[i];
        if(sum[i + 1] < 0 and v.back().first < -sum[i + 1])
        {
            v.push_back(make_pair(-sum[i + 1], i + 1));
        }
        if(tmp <= 0) flag = true;
    }
    if(flag == false and cycle >= 0)
    {
        cout << -1 << endl;
        return 0;
    }
    v.erase(v.begin());
    sort(v.begin(), v.end());
    ll ans = 0;
    while(S > T)
    {
        ll pos = lower_bound(v.begin(), v.end(), make_pair(S - T, 0LL)) - v.begin();
        if(pos < v.size())
        {
            ans += v[pos].second;
            break;
        }
        S += cycle;
        ans += D;
    }
    cout << ans << endl;
    return 0;
}

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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
  for(auto &e:u) fill_v<T>(e,v...);
}

int main(void) {
    int64 S, T, D;
    cin >> S >> T >> D;
    vector<int64> w(D), sum(D+1, 0);
    int64 mn = 0;
    REP(i, D) {
        cin >> w[i];
        sum[i+1] += sum[i] + w[i];
        chmin(mn, sum[i+1]);
    }
    if (abs(mn) < S-T && sum[D] >= 0) {
        cout << -1 << endl;
    } else {
        int64 res = (S-T+mn)/abs(sum[D]);
        chmax(res, 0);
        S -= T;
        S -= res * abs(sum[D]);
        res *= D;
        int64 idx = 0;
        while (S > 0) {
            S += w[idx++];
            idx %= D;
            res++;
        }
        cout << res << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll s, t, d;
  cin >> s >> t >> d;
  vector<ll> w(d);
  for (int i = 0; i < d; i++)
    cin >> w[i];
  ll minw = 0;
  ll sum = 0;
  for (int i = 0; i < d; i++) {
    sum += w[i];
    minw = min(minw, sum);
  }

  ll s2 = s;
  for (int i = 0; i < d; i++) {
    s2 += w[i];
    if (s2 <= t) {
      cout << i + 1 << endl;
      return 0;
    }
  }

  if (sum >= 0) {
    cout << -1 << endl;
    return 0;
  }
  sum = -sum;
  ll e = (s - t + minw + sum - 1) / sum;
  ll ans = e * d;
  s -= sum * e;
  for (int i = 0; i < d; i++) {
    s += w[i];
    if (s <= t) {
      cout << ans + i + 1 << endl;
      return 0;
    }
  }

  return 0;
}