Problem p03566 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fi first
#define se second
#define show(x) cout << #x << "=" << x << "\n"
typedef long long ll;
template <typename T>
void print_to(std::ostream &os, const char *, const char *tail, const T &fst) {
  os << fst << tail;
}
template <typename Fst, typename... Rst>
void print_to(std::ostream &os, const char *del, const char *tail,
              const Fst &fst, const Rst &...rst) {
  os << fst << del;
  print_to(os, del, tail, rst...);
}
template <typename Iter>
void print_to_(std::ostream &os, const char *del, const char *tail, Iter bgn,
               Iter end) {
  for (Iter it = bgn; it != end;) {
    os << *it;
    if (++it != end)
      os << del;
    else
      os << tail;
  }
}
template <typename Fst, typename... Rst>
void println(const Fst &fst, const Rst &...rst) {
  print_to(std::cout, "\n", "\n", fst, rst...);
}
template <typename Fst, typename... Rst>
void print(const Fst &fst, const Rst &...rst) {
  print_to(std::cout, " ", "\n", fst, rst...);
}
template <typename Iter> void println_(Iter bgn, Iter end) {
  print_to_(std::cout, " ", "\n", bgn, end);
}
using namespace std;
// const int MOD=(ll)1e10+7;
const int dy[] = {1, 0, -1};
const int dx[] = {1, 0, -1};
int n, m, h, w;
string s;
int t[101], v[101];
double minv[20001];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
#ifdef LOCAL_DEFINE
  FILE *stream1;
  // FILE *stream2;
  stream1 = freopen("in", "r", stdin);
  // stream2=freopen("out","w",stdout);
  if (stream1 == NULL)
    return 0;
    // if(stream2==NULL)return 0;
#endif
  cin >> n;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
    t[i] *= 2;
    sum += t[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int t1[101];
  for (int i = 0; i < n; i++) {
    if (i == 0) {
      t1[i] = t[0];
      continue;
    }
    t1[i] = t1[i - 1] + t[i];
  }
  int t2[101];
  for (int i = n - 1; i >= 0; i--) {
    if (i == n - 1) {
      t2[i] = sum - t[i];
      continue;
    }
    t2[i] = t2[i + 1] - t[i];
  }
  int now = 0;
  for (int i = 0; i <= sum; i++) {
    if (i > t1[now])
      now++;
    if (i == 0) {
      minv[i] = 0;
      continue;
    }
    minv[i] = min(minv[i - 1] + 0.5, (double)v[now]);
  }
  // for(int i=0;i<=sum;i++){
  // cout<<minv[i]<<" ";
  //}
  // cout<<endl;
  now = n - 1;
  for (int i = sum; i >= 0; i--) {
    if (i < t2[now])
      now--;
    if (i == sum) {
      minv[i] = 0;
      continue;
    }
    minv[i] = min({minv[i + 1] + 0.5, (double)v[now], minv[i]});
  }
  /*for(int i=0;i<=sum;i++){
    printf("%.2f ",minv[i]);
  }
  printf("\n");*/
  double ans = 0;
  for (int i = 0; i < sum; i++) {
    if (i == 0) {
      ans += (minv[i + 1] + minv[i]) / 4;
    } else {
      ans += (minv[i] + minv[i + 1]) / 4;
    }
  }
  printf("%.10f\n", ans);
#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s.\n";
  fclose(stream1);
  // fclose(stream2);
#endif
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define show(x) cout << #x << "=" << x << "\n"
typedef long long ll;
template <typename T>
void print_to(std::ostream &os, const char *, const char *tail, const T &fst) {
  os << fst << tail;
}
template <typename Fst, typename... Rst>
void print_to(std::ostream &os, const char *del, const char *tail,
              const Fst &fst, const Rst &...rst) {
  os << fst << del;
  print_to(os, del, tail, rst...);
}
template <typename Iter>
void print_to_(std::ostream &os, const char *del, const char *tail, Iter bgn,
               Iter end) {
  for (Iter it = bgn; it != end;) {
    os << *it;
    if (++it != end)
      os << del;
    else
      os << tail;
  }
}
template <typename Fst, typename... Rst>
void println(const Fst &fst, const Rst &...rst) {
  print_to(std::cout, "\n", "\n", fst, rst...);
}
template <typename Fst, typename... Rst>
void print(const Fst &fst, const Rst &...rst) {
  print_to(std::cout, " ", "\n", fst, rst...);
}
template <typename Iter> void println_(Iter bgn, Iter end) {
  print_to_(std::cout, " ", "\n", bgn, end);
}
using namespace std;
// const int MOD=(ll)1e10+7;
const int dy[] = {1, 0, -1};
const int dx[] = {1, 0, -1};
int n, m, h, w;
string s;
int t[101], v[101];
double minv[40004];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
#ifdef LOCAL_DEFINE
  FILE *stream1;
  // FILE *stream2;
  stream1 = freopen("in", "r", stdin);
  // stream2=freopen("out","w",stdout);
  if (stream1 == NULL)
    return 0;
    // if(stream2==NULL)return 0;
#endif
  cin >> n;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
    t[i] *= 2;
    sum += t[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int t1[101];
  for (int i = 0; i < n; i++) {
    if (i == 0) {
      t1[i] = t[0];
      continue;
    }
    t1[i] = t1[i - 1] + t[i];
  }
  int t2[101];
  for (int i = n - 1; i >= 0; i--) {
    if (i == n - 1) {
      t2[i] = sum - t[i];
      continue;
    }
    t2[i] = t2[i + 1] - t[i];
  }
  int now = 0;
  for (int i = 0; i <= sum; i++) {
    if (i > t1[now])
      now++;
    if (i == 0) {
      minv[i] = 0;
      continue;
    }
    minv[i] = min(minv[i - 1] + 0.5, (double)v[now]);
  }
  // for(int i=0;i<=sum;i++){
  // cout<<minv[i]<<" ";
  //}
  // cout<<endl;
  now = n - 1;
  for (int i = sum; i >= 0; i--) {
    if (i < t2[now])
      now--;
    if (i == sum) {
      minv[i] = 0;
      continue;
    }
    minv[i] = min({minv[i + 1] + 0.5, (double)v[now], minv[i]});
  }
  /*for(int i=0;i<=sum;i++){
    printf("%.2f ",minv[i]);
  }
  printf("\n");*/
  double ans = 0;
  for (int i = 0; i < sum; i++) {
    if (i == 0) {
      ans += (minv[i + 1] + minv[i]) / 4;
    } else {
      ans += (minv[i] + minv[i + 1]) / 4;
    }
  }
  printf("%.10f\n", ans);
#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s.\n";
  fclose(stream1);
  // fclose(stream2);
#endif
  return 0;
}