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;
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);
    else 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<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];
  while(0<s&&c<1e6)s+=w[c++%d];
  if(c==1e6)c=-1;
  cout<<c<<endl;
  return 0;
}

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

using namespace std;

using lint = long long;

lint INF = numeric_limits<lint>::max();

int main() {
    lint s, t, d;
    cin >> s >> t >> d;

    vector<lint> w(d);
    for (auto& a : w) cin >> a;
    lint wsum = accumulate(w.begin(), w.end(), 0LL);

    lint ans = INF;
    if (wsum >= 0) {
        // 最初の1週だけ見る
        for (int i = 0; i <= d; ++i) {
            if (s <= t) {
                ans = i;
                break;
            }
            if (i < d) s += w[i];
        }
    } else {
        lint ok = 100001, ng = -1;
        while (ok - ng > 1) {
            lint mid = (ok + ng) / 2;
            lint ss = s + mid * wsum;

            bool judge = false;
            for (int i = 0; i <= d; ++i) {
                if (ss <= t) {
                    ans = min(ans, mid * d + i);
                    judge = true;
                }
                if (i < d) ss += w[i];
            }

            (judge ? ok : ng) = mid;
        }
    }

    cout << (ans == INF ? -1 : ans) << endl;
    return 0;
}

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

int main(void) {
  ll i, j, s, t, d, ans = -1, min = 10e17;
  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(sum[i] < min) min = sum[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 + min - sum[d - 1] - 1) / -sum[d - 1] * d;
  s = -min;
  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 + 1);
  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];
  ll ans=0,tp=s;
  for(int i=0;tp>0&&i<2*d;i++)tp+=w[i%d],ans++;
  if(tp>0){
    if(c<0){
      c=-c;
      ans=(s/c-1)*d;
      s=s%c+c;
      for(int i=0;s>0;i++)s+=w[i%d],ans++;
    }
    else ans=-1;
  }
  cout<<ans<<endl;
  return 0;
}

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

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

    long long dif = 0;
    long long nin = 0;
    for (int i = 0; i < d; i++) {
        dif += ws[i];
        nin = min(nin, dif);
    }
    long long cnt = 0;
    long long ss = s;
    for (int i = 0; i < d; i++) {
        ss += ws[i];
        if (ss <= t) {
            cout << i + 1 << endl;
            return 0;
        }
    }
    if (dif >= 0) {
        cout << -1 << endl;
        return 0;
    }
    int l = -1, r = 1e9;
    while (l < r - 1) {
        int m = (l + r) / 2;
        if (s + dif * m + nin <= t) {
            r = m;
        } else {
            l = m;
        }
    }
    cnt = 1LL * d * r;
    s += dif * r;
    for (int i = 0; i < d; i++) {
        s += ws[i];
        cnt++;
        if (s <= t) {
            cout << cnt << endl;
            return 0;
        }
    }
}

C++: #include<bits/stdc++.h>
#define MAX 100002
#define inf 1<<29
#define linf (1e14)
#define eps (1e-8)
#define Eps (1e-12)
#define mod 1000000007
#define pi acos(-1.0)
#define phi (1.0+sqrt(5.0))/2.0
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define pd(a) printf("%.10f\n",(double)(a))
#define pld(a) printf("%.10Lf\n",(ld)(a))
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(a)-1;(b)<=i;i--)
#define Unique(v) v.erase(unique(all(v)),v.end())
#define equals(a,b) (fabs((a)-(b))<eps)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<int,double> pid;
typedef pair<double,int> pdi;
typedef pair<double,double> pdd;
typedef vector<int> vi;
typedef vector<pii> vpi;

ll s,t,d,w[MAX];

ll solve(){
  ll sum = 0, ans = linf,sub = 0;
  FOR(i,0,d)sum+=w[i];
  FOR(i,0,d){
    sub+=w[i];
    if(sub+s<=t){
      ans = i+1;
      break;
    }
    if(sum<0){
      ll c = s-t+sub;
      if((c%abs(sum))==0)ans = min(ans,i+1+d*(c/abs(sum)));
      else ans = min(ans,i+1+d*(c/abs(sum)+1));
    }
  }
  if(ans==linf)return -1;
  return ans;
}

int main()
{
  cin>>s>>t>>d;
  FOR(i,0,d)cin>>w[i];
  cout<<solve()<<endl;
  return 0;
}