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 int long long
signed main(){
  int s,t,d;
  cin>>s>>t>>d;
  int w[d];
  for(int i=0;i<d;i++) cin>>w[i];
  int a[d+1];
  a[0]=0;
  for(int i=0;i<d;i++) a[i+1]=a[i]+w[i];
  int k=*min_element(a,a+d+1);
  if(s+k>t&&a[d]>=0){
    cout<<-1<<endl;
    return 0;
  }
  for(int i=0;i<=d;i++){
    if(s+a[i]<=t) {
      cout<<i<<endl;
      return 0;
    }
  }
  int l=0,r=1000000;
  while(l+1<r){
    int m=(l+r)/2;
    //cout<<m<<":"<<s+a[d]*m<<" "<<t<<endl;
    int x=0;
    for(int i=0;i<=d;i++){
      if(s+a[d]*m+a[i]<=t) {
	x=1;
	break;
      }
    }
    if(x) r=m;
    else l=m;
  }
  //cout<<r<<endl;
  int ans=r*d;
  for(int i=0;i<=d;i++){
    if(s+a[d]*r+a[i]<=t) break;
    ans++;
  }
  cout<<ans<<endl;
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    ll s, t, d;
    cin >> s >> t >> d;
    vector<ll> v(d);
    for (auto &&i : v) scanf("%lld", &i);
    ll w = accumulate(v.begin(),v.end(), 0LL);
    if(w >= 0){
        for (int i = 0; i < d; ++i) {
            s += v[i];
            if(s <= t){
                cout << i+1 << "\n";
                return 0;
            }
        }
        puts("-1");
        return 0;
    }
    w = -w;
    ll ans = INF<ll>;
    for (ll i = 0; i < d; ++i) {
        if(i) s += v[i-1];
        if(s <= t) ans = min(ans, i);
        else ans = min(ans, i+(s-t+w-1)/w*d);
    }
    if(ans == INF<ll>) return 1;
    cout << ans << "\n";
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=203,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    ll S,T,D;cin>>S>>T>>D;
    vector<ll> A(D);
    ll sum=0,mini=INF;
    for(int i=0;i<D;i++) cin>>A[i];
    
    for(int i=0;i<D;i++){
        sum+=A[i];
        if(S+sum<=T){
            cout<<i+1<<endl;
            return 0;
        }
        mini=min(mini,sum);
    }
    
    if(sum>=0) cout<<-1<<endl;
    else{
        ll rem=S-T+mini;
        sum*=(-1);
        ll need=(rem+sum-1)/sum;
        
        rem=S-T-sum*need;
        if(rem<=0){
            cout<<need*D<<endl;
            return 0;
        }
        for(int i=0;i<D;i++){
            rem+=A[i];
            if(rem<=0){
                cout<<need*D+i+1<<endl;
                return 0;
            }
        }
    }
    
}

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

typedef int64_t Int;
#define all(x) (x).begin(), (x).end()
 
const double EPS = 1e-10;
const Int INF = 1e18;
const int inf = 1e9;
const Int mod = 1e9+7;

bool print_space_enable = false;
void print() { 
    cout << '\n'; 
    print_space_enable = false;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
    if (print_space_enable) cout << " ";
    cout << fixed << setprecision(15) << head;
    print_space_enable = true;
    print(std::forward<Tail>(tail)...);
}

template<typename T>
void print(vector<T> v) {
    for (size_t i = 0; i < v.size(); i++) {
        if (i > 0) std::cout << " ";
        std::cout << v[i];
    }
    std::cout << std::endl;
}

int main() {
    Int s, t, d;
    cin >> s >> t >> d;
    vector<Int> w(d);
    for (Int i = 0; i < d; i++) {
        cin >> w[i];
    }
    Int sum = accumulate(all(w), (Int)0);
    if (sum >= 0) {
        for (Int i = 0; i < d; i++) {
            s += w[i];
            if (s <= t) {
                print(i + 1);
                return 0;
            }
        }
        print(-1);
        return 0;
    }
    Int mi = 0;
    Int x = 0;
    for (Int i = 0; i < d; i++) {
        x += w[i];
        mi = min(mi, x);
    }
    Int a = max((s - t + mi) / abs(sum), (Int)0);
    if ((s - t + mi) > 0 and (s - t + mi) % abs(sum) != 0) a++;
    Int cur = s + a * sum;
    Int res = a * d;
    for (Int i = 0; i < d; i++) {
        res++;
        cur += w[i];
        if (cur <= t) {
            print(res);
            return 0;
        }
    }
    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<d;i++)tp+=w[i],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>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int s,t,d;
ll w[100001];
ll sum=0;
ll af[100001];

int main(void){
	scanf("%d%d%d",&s,&t,&d);
	for(int i=0;i<d;i++){
		scanf("%lld",&w[i]);
		sum-=w[i];
	}
	ll res=-2;
	ll now=s;
	for(int i=0;i<d;i++){
		now+=w[i];
		if(now<=t){
			printf("%d\n",i+1);
			return 0;
		}else if(sum>0LL){
			ll rest=now-t;
			ll val=(ll)i+(rest+sum-1LL)/sum*d;
			if(res==-2){
				res=val;
			}else{
				res=min(res,val);
			}
		}
	}
	printf("%lld\n",res+1LL);
	return 0;
}