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 <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;
	}

	int 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("%d\n",ans);
				return 0;
			}
			pre_loc = V[i].loc;
		}
		tmp -= V[V.size()-1].minus_value;
	}

	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>=0){
        cout<<-1<<endl;
        return 0;
    }
    ll ans=0;
    int 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 <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,int arg_loc){
		minus_value = arg_minus_value;
		loc = arg_loc;
	}
	ll minus_value;
	int loc;
};


int main(){

	int D;
	ll S,T;
	scanf("%lld %lld %d",&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;
	}

	int pre_loc = -1;
	ll ans = D;

	while(true){

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

			tmp -= V[i].minus_value;

			if(V[i].loc <= pre_loc){
				ans += (V[i].loc+D)-pre_loc;
			}else{
				ans += V[i].loc-pre_loc;
			}
			if(tmp <= T){
				printf("%d\n",ans);
				return 0;
			}
			pre_loc = V[i].loc;
		}
	}

	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("no\n");
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)
#define int long long
main(){
  int S,T,D,sum=0,ans=0,p=0,d;
  cin>>S>>T>>D;
  int t=S;
  int a[D];
  r(i,D){
    cin>>a[i],sum+=a[i];
    if(p>sum){
      p=sum;
      d=D;
    }
  }
  sum-=p;
  ///////////
  if(p>=0){
    cout<<-1<<endl;
    return 0;
  }
  r(o,5){r(i,D){
    t+=a[i];
    if(t<=T){
      cout<<i+1+o*D<<endl;
      return 0;
    }
  }
    /////////
    if(o==4&&t>=S){
      cout<<-1<<endl;
      return 0;
    }
  }/////////////
  while(1){
    S+=p;
    ans+=d;
    if(S<=T){
      S-=p;
      ans-=d;
      break;
    }
    S+=sum;
    ans+=D-d;
    if(S<=T){
      S-=sum;
      ans-=D-d;
      break;
    }
  }
  r(i,D){
    S+=a[i];
    if(S<=T){
      cout<<ans+i+1<<endl;
      return 0;
    }
  }
  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] * d;
  s %= -sum[d - 1];
  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;
}