Problem p02917 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ff first
#define ss second
#define MAX 10005
#define endl "\n"
#define mp make_pair
#define pb push_back
#define int long long
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define vi vector<int>
#define mii map<int, int>
#define pii pair<int, int>
#define len(x) (x.size())
#define all(a) a.begin(), a.end()
#define f(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) for (int i = 0; i < a; i++)
#define debug(x) cerr << #x << ": " << x << endl
#define T                                                                      \
  int tc;                                                                      \
  cin >> tc;                                                                   \
  for (int t = 1; t <= tc; t++)
#define set_arr(arr, i) memset(arr, i, sizeof(arr))
#define trace1(x) cerr << #x << ": " << x << endl;
#define trace2(x, y)                                                           \
  cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                                                        \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << endl;
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl;
using namespace std;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  vi b(n - 1);
  rep(i, n - 1) { cin >> b[i]; }
  vi a(n);
  a[0] = b[0];
  f(i, 1, n - 1) { a[i] = min(b[i], b[i - 1]); }
  a[n - 1] = max(b[n - 2], a[n - 1]);
  int ans = 0;
  rep(i, n) ans += a[i];
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ff first
#define ss second
#define MAX 10005
#define endl "\n"
#define mp make_pair
#define pb push_back
#define int long long
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define vi vector<int>
#define mii map<int, int>
#define pii pair<int, int>
#define len(x) (x.size())
#define all(a) a.begin(), a.end()
#define f(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) for (int i = 0; i < a; i++)
#define debug(x) cerr << #x << ": " << x << endl
#define T                                                                      \
  int tc;                                                                      \
  cin >> tc;                                                                   \
  for (int t = 1; t <= tc; t++)
#define set_arr(arr, i) memset(arr, i, sizeof(arr))
#define trace1(x) cerr << #x << ": " << x << endl;
#define trace2(x, y)                                                           \
  cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                                                        \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << endl;
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl;
using namespace std;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n;
  cin >> n;
  vi b(n - 1);
  rep(i, n - 1) { cin >> b[i]; }
  vi a(n);
  a[0] = b[0];
  f(i, 1, n - 1) { a[i] = min(b[i], b[i - 1]); }
  a[n - 1] = max(b[n - 2], a[n - 1]);
  int ans = 0;
  rep(i, n) ans += a[i];
  cout << ans;
  return 0;
}