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>
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)-1)*d;
    sub-=(sub/(-sum)-1)*(-sum);
    rep(i,d){
        if(sub<=0){
            cout<<ans<<endl;
            return 0;
        }
        sub+=w[i];
        ++ans;
    }
    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;
using ld = long double;
using P = pair<ll, ll>;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const ll MOD = 1000000007;
const ll INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
  for(ll i=0;i<(ll)(s.size());++i) {
    cout << s[i];
    if(i == (ll)(s.size())-1) cout << endl;
    else cout << " ";
  }
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  ll s, t, d; cin >> s >> t >> d;
  vi w(d);
  ll su = 0;
  ll mi = LINF;
  ll argmi = -1;
  for(ll i=0;i<d;++i) {
    cin >> w[i];
    su += w[i];
    if(mi > min(mi, su)) {
      mi = su;
      argmi = i;
    }
  }
  ll tmp1 = s - t;
  if(tmp1 + mi <= 0) {
    for(ll i=0;i<d;++i) {
      tmp1 += w[i];
      if(tmp1 <= 0) {
        cout << i + 1 << endl;
        break;
      }
    }
    return 0;
  }
  if(su >= 0) {
    cout << -1 << endl;
    return 0;
  }
  ll tmp2 = tmp1 + mi;
  ll cnt = (tmp2 - 1) / (-su) + 1;
  tmp2 = tmp1 + cnt * su;
  ll ans = cnt * d;
  for(ll i=0;i<d;++i) {
    ans++;
    tmp2 += w[i];
    if(tmp2 <= 0) {
      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=1e11;
  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++){
    s+=w[i];
    // if(s<=0)ans=min(ans,i+1);
     if(c>0)ans=min(ans,i+(s+c-1)/c*d+1);
  }
  if(ans==1e11)ans=-1;
  cout<<ans<<endl;
  return 0;
}

C++: #include <stdio.h>
typedef long long ll;

int main(void) {
  ll i, j, s, t, d, ans = -1;
  scanf("%lld%lld%lld", &s, &t, &d);
  ll sum[d], w[d];
  for(i = 0; i < d; ++i) {
    scanf("%lld", &w[i]);
    if(i) sum[i] = sum[i - 1] + w[i];
    else sum[i] = w[i];
    if(s + sum[i] <= t) {
      printf("%lld\n", i + 1);
      return 0;
    }
  }
  if(sum[d - 1] >= 0) {
    printf("-1\n");
    return 0;
  }
  s -= t;
  ans = (s / -sum[d - 1]) * d;
  s %= -sum[d - 1];
  if(!s) {
    printf("%lld\n", ans);
    return 0;
  }
  for(i = 0; i < d; ++i) {
    if(-sum[i] >= s) {
      ans += i + 1;
      printf("%lld\n", ans);
      return 0;
    }
  }
  printf("%lld\n", ans + d);
  return 0;
}

C++: #include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i))
#define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
#define double long double
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template<class T> bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T> bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}
template<class T> T div_floor(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>=0 ? a/b : (a+1)/b-1;
}
template<class T> T div_ceil(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>0 ? (a-1)/b+1 : a/b;
}

constexpr lint mod = 1e9+7;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

int main(){
    lint s, t; int d;
    scanf("%lld%lld%d", &s, &t, &d);
    lint w[d+1];
    w[0] = 0;
    rep(i, d) scanf("%lld", &w[i+1]);
    partial_sum(w, w+d+1, w);
    rep(i, d+1)if(s+w[i] <= t){
        printf("%d\n", i);
        return 0;
    }
    if(w[d] >= 0){
        puts("-1");
        return 0;
    }

    lint ans = INF;
    rep(i, d){
        lint k = div_ceil(t-s-w[i], w[d]);
        chmin(ans, k*d + i);
    }
    printf("%lld\n", ans);
}