Source Code
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.stream.LongStream;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author dauom
 */
public class Main {
  public static void main(String[] args) {
    InputStream inputStream = System.in;
    OutputStream outputStream = System.out;
    InputReader in = new InputReader(inputStream);
    PrintWriter out = new PrintWriter(outputStream);
    TaskH solver = new TaskH();
    solver.solve(1, in, out);
    out.close();
  }

  static class TaskH {
    public final void solve(int testNumber, InputReader in, PrintWriter out) {
      int n = in.nextInt();
      long[] a = in.nextLongArray(n);
      long[] b = in.nextLongArray(n);
      if (LongStream.of(a).sum() > LongStream.of(b).sum()) {
        out.println(-1);
        return;
      }

      long bullets = 0;
      long left = 0, lastUncleared = -1;
      long time = n - 1;
      for (int i = 0; i < n; i++) {
        bullets += b[i];
        if (left <= bullets && lastUncleared != -1 && ((i - lastUncleared) * 2 <= n - 1 - lastUncleared || n - 1 == i)) {
          time += i - lastUncleared;
          if (i != n - 1) {
            time += i - lastUncleared;
          }
          time += left;
          bullets -= left;
          left = 0;
          lastUncleared = -1;
        }
        if (bullets >= a[i]) {
          bullets -= a[i];
          time += a[i];
          a[i] = 0;
        } else {
          a[i] -= bullets;
          time += bullets;
          bullets = 0;
          if (lastUncleared == -1) {
            lastUncleared = i;
          }
        }
        left += a[i];
      }

      out.println(time);
    }

  }

  static final class InputReader {
    private final InputStream stream;
    private final byte[] buf = new byte[1 << 18];
    private int curChar;
    private int numChars;

    public InputReader() {
      this.stream = System.in;
    }

    public InputReader(final InputStream stream) {
      this.stream = stream;
    }

    private int read() {
      if (this.numChars == -1) {
        throw new UnknownError();
      } else {
        if (this.curChar >= this.numChars) {
          this.curChar = 0;

          try {
            this.numChars = this.stream.read(this.buf);
          } catch (IOException ex) {
            throw new InputMismatchException();
          }

          if (this.numChars <= 0) {
            return -1;
          }
        }

        return this.buf[this.curChar++];
      }
    }

    public final int nextInt() {
      int c;
      for (c = this.read(); isSpaceChar(c); c = this.read()) {
      }

      byte sgn = 1;
      if (c == 45) { // 45 == '-'
        sgn = -1;
        c = this.read();
      }

      int res = 0;

      while (c >= 48 && c <= 57) { // 48 == '0', 57 == '9'
        res *= 10;
        res += c - 48; // 48 == '0'
        c = this.read();
        if (isSpaceChar(c)) {
          return res * sgn;
        }
      }

      throw new InputMismatchException();
    }

    public final long nextLong() {
      int c;
      for (c = this.read(); isSpaceChar(c); c = this.read()) {
      }

      byte sgn = 1;
      if (c == 45) { // 45 == '-'
        sgn = -1;
        c = this.read();
      }

      long res = 0;

      while (c >= 48 && c <= 57) { // 48 == '0', 57 == '9'
        res *= 10L;
        res += c - 48; // 48 == '0'
        c = this.read();
        if (isSpaceChar(c)) {
          return res * sgn;
        }
      }
      throw new InputMismatchException();
    }

    private static boolean isSpaceChar(final int c) {
      return c == 32 || c == 10 || c == 13 || c == 9
          || c == -1; // 32 == ' ', 10 == '\n', 13 == '\r', 9 == '\t'
    }

    public final long[] nextLongArray(final int n) {
      long[] arr = new long[n];
      for (int i = 0; i < n; i++) {
        arr[i] = nextLong();
      }
      return arr;
    }

  }
}

Copy
Shooting Balloons dauom
Java 11
71 ms
13.9 MB
Wrong Answer