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"
#include <random>
#define REP(i, n) for (ll i = 0; i < ll(n); ++i)
#define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < ll(n); ++i)
#define RFOR(i, m, n) for (ll i = ll(n) - 1; i >= ll(m); --i)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

constexpr int dx[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
constexpr int dy[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };

using namespace std;

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <class T> bool chmin(T& a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll s, t, d;
  cin >> s >> t >> d;
  vl v(d);
  REP(i, d) cin >> v[i];
  vl a(d + 1);
  REP(i, d) {
    a[i + 1] = a[i] + v[i];
    if (a[i + 1] <= t - s) {
      cout << i + 1 << endl;
      return 0;
    }
  }
  if (a[d] >= 0) {
    cout << -1 << endl;
    return 0;
  }
  ll r = -a[d];
  ll ans = INF * INF;
  REP(i, d) {
    ll k = ((s - t) + a[i] + r - 1) / r;
    chmin(ans, k * d + i);
  }

  cout << ans << endl;
}

C++: #include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<29;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-11;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

ll s,t,n;
vl a;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>s>>t>>n;
	a=vl(n);
	ll sum=0,mn=0;
	for(int i=0;i<n;i++){
		cin>>a[i];
		sum+=a[i];
		mn=min(mn,sum);
	}
	ll d=s-t,res=(sum>=0?0:max(0LL,(d+mn)/(-sum)));
	d+=sum*res;
	res*=n;
	for(int i=0;i<3*n;i++){
		d+=a[i%n];
		if(d<=0){
			cout<<res+i+1<<endl;
			return 0;
		}
	}
	cout<<-1<<endl;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    ll s,t,n; cin >> s >> t >> n;
    vector<ll> a(n+1,0);
    for(int i=0;i<n;i++){
        cin >> a[i+1];
        a[i+1]+=a[i];
    }
    ll res=1e18;
    ll dif=s-t;
    for(int i=0;i<=n;i++){
        ll w=dif+a[i];
        if(w<=0){
            res=min(res,(ll)i);
        }
        else{
            if(a[n]>=0)continue;
            res=min(res,(ll)i+(w-a[n]-1)/(-a[n])*n);
        }
    }
    if(res>=1e18){
        printf("-1\n");
    }
    else{
        printf("%lld\n",res );
    }
}

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 = (s + min) % -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;
}

C++: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;



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

	cin >> N >> M >> K;
	vector<long long int>v(K);
	for (auto &i : v)cin >> i;
	long long int mi = v[0];
	for (int i = 1; i < K; i++) {
		v[i] += v[i - 1];
		mi = min(mi, v[i]);
	}
	if (N + mi > M&&v.back() >= 0) {
		cout << -1 << endl;
		return 0;
	}
	long long int ans = 1;
	while (1) {
		if (N + mi > M) {
			ans += K;
			N += v.back();
		}
		else {
			for (auto i : v) {
				if (N + i > M)ans++;
				else {
					cout << ans << endl;
					return 0;
				}
			}
		}
	}
}