Source Code
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define sz(s)    (int)(s.size())
#define endl "\n"
#define PI acos(-1)
#define all(a) a.begin(), a.end()

void Open() {
#ifndef ONLINE_JUDGE
  //freopen("input.txt", "r", stdin);
  //freopen("out.txt", "w", stdout);
#endif  !ONLINEJUDGE
  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}
int dx[]{ 1, -1, 0, 0, 1, 1, -1, -1 };
int dy[]{ 0, 0, 1, -1, 1, -1, 1, -1 };
const int OO = 0x3f3f3f3f;

int dp[300005];
int n;

ll v[300005], b[300005];
int solve(int x)
{
  if (x == n + 1)
    return 0;
  int& ret = dp[x];
  if (~ret)return ret;
  ret = 1e9;
  if (b[x] >= v[x]) {
    ret = solve(x + 1) + 1;
  }

  else
  {
    ll cnt = 0;
    for (int i = x; i <= n; i++)
    {
      cnt++;
      if (b[i] >= v[i])
      {
        if (i != n) {
          return ret = min((ll)ret, solve(i + 1) + (cnt*3) -2);
        }
        else {
          return ret = min((ll)ret, solve(i + 1) + (cnt) +(cnt -1));
        }
      }
    }
  }
  return ret;


}

int main()
{
  Open();
  cin >> n;
  memset(dp, -1, sizeof dp);
  for (int i = 1; i <= n; i++)cin >> v[i], v[i] += v[i - 1];
  for (int i = 1; i <= n; i++)cin >> b[i], b[i] += b[i - 1];

  b[0] = -1;
  v[0] = 0;
  if (v[n] > b[n])cout<<("-1");
  else cout << (ll)solve(1)-1+v[n];

}
Copy
a matb3aa
GNU G++17
1 ms
1.5 MB
Wrong Answer