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++: /*  -*- coding: utf-8 -*-
 *
 * 2846.cc: Slimming Plan
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_D = 100000;
const long long LINF = 1LL << 62;

/* typedef */

typedef long long ll;

/* global variables */

ll wsums[MAX_D + 1];

/* subroutines */

inline void setmin(ll &a, ll b) { if (a > b) a = b; }
inline void setmax(ll &a, ll b) { if (a < b) a = b; }

/* main */

int main() {
  ll s, t;
  int d;
  scanf("%lld%lld%d", &s, &t, &d);
  ll dt = -(t - s);

  ll maxw = 0;
  for (int i = 0; i < d; i++) {
    int wi;
    scanf("%d", &wi);
    wsums[i + 1] = wsums[i] - wi;
    setmax(maxw, wsums[i + 1]);
  }
  //printf("dt=%lld, maxw=%lld, wsum=%lld\n", dt, maxw, wsums[d]);

  if (maxw >= dt) {
    int k = 0;
    for (; k <= d; k++)
      if (wsums[k] >= dt) break;
    printf("%d\n", k);
  }
  else if (wsums[d] > 0) {
    ll m = (dt - maxw + wsums[d] - 1) / wsums[d];
    dt -= m * wsums[d];
    int k = 0;
    for (; k <= d; k++)
      if (wsums[k] >= dt) break;
    printf("%lld\n", m * d + k);
  }
  else
    puts("-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<d;i++)tp+=w[i],ans++;
  if(tp>0){
    if(c<0){
      ans=s/-c*d;
      s%=-c;
      for(int i=0;s>0;i++)s+=w[i],ans++;
    }
    else ans=-1;
  }
  cout<<ans<<endl;
  return 0;
}

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

using ll = long long;

constexpr ll inf = 1e18;

int main() {
    ll s, t, d; cin >> s >> t >> d;
    vector<ll> w(d), sum(d + 1);
    for(int i = 0; i < d; ++i) {
        cin >> w[i];
        sum[i + 1] = sum[i] + w[i];
    }

    ll mini = inf;
    for(int i = 0; i <= d; ++i) {
        if(s + sum[i] <= t) {
            cout << i << endl;
            return 0;
        }
        mini = min(mini, sum[i]);
    }

    if(sum[d] >= 0) {
        cout << -1 << endl;
        return 0;
    }

    const ll cnt = (s - t + mini - sum[d] - 1) / (-sum[d]);
    ll ans = cnt * d;
    s += cnt * sum[d];
    for(int i = 0; i <= d; ++i) {
        if(s + sum[i] <= t) {
            ans += i;
            break;
        }
    }

    cout << ans << endl;
}

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-2*c)/c*d;
      s-=(s-2*c)/c*c;
      for(int i=0;s>0;i++)s+=w[i%d],ans++;
    }
    else ans=-1;
  }
  cout<<ans<<endl;
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#ifdef _DEBUG_
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl
#else
#define dump(val)
#endif

using namespace std;

typedef long long int ll;

template<typename T>
vector<T> make_v(size_t a, T b) {
    return vector<T>(a, b);
}
template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    ll s, t, d;
    cin >> s >> t >> d;
    vector<ll> w(d);
    ll sum = 0;
    ll mn = 1LL << 60;
    s -= t;
    REP(i, 0, d) {
        cin >> w[i];
        sum += w[i];
        mn = min(mn, sum);
        if (s + sum <= 0) {
            cout << i + 1 << endl;
            return 0;
        }
    }
    if (sum >= 0) {
        cout << -1 << endl;
        return 0;
    }

    dump(mn);
    dump(sum);
    ll ans = max((s + mn - sum) / (-sum), 0LL);
    dump(ans);
    s += sum * ans;
    ans *= d;
    if (s <= 0) {
        cout << ans << endl;
        return 0;
    }
    dump(s);
    REP (i, 0, d) {
        s += w[i];
        if (s <= 0) {
            cout << ans + i + 1 << endl;
            return 0;
        }
    }
    dump(s);
    return 0;
}