Problem p02603 (C++)

Buggy submission (status: Runtime Error):
/*input
7
100 130 130 130 115 115 150
*/

// #include<bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstdint>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;
long long mod = 1e9 + 7;
long long INF = 1e7;
double EPS = 1e-12;
const long long lim = 2e5 + 100;
typedef long long int lld;
typedef long long ll;

#define Error(x...)                                                            \
  {                                                                            \
    cout << "(" << #x << ")"                                                   \
         << " = ( ";                                                           \
    printIt(x);                                                                \
  }
template <typename T1> void printIt(T1 t1) { cout << t1 << " )" << endl; }
template <typename T1, typename... T2> void printIt(T1 t1, T2... t2) {
  cout << t1 << " , ";
  printIt(t2...);
}

#define popcount __builtin_popcountll

ll powP(ll x, ll y) {
  ll ans = 1, po = x % mod;
  while (y > 0) {
    if (y & 1)
      ans = ans * po % mod;
    y >>= 1;
    po = po * po % mod;
  }
  return ans;
}

vector<pair<ll, ll>> V;
ll Array[lim], C[lim];
vector<pair<ll, ll>> Allstocks;

int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);

  lld T, i, p, j, l, e, r, b, c, k, m, q, a, d, w, x, y, u, v, z, t, curr, prev,
      sum = 0, val, countA = 0, indicator = 0, pos, h, minA, maxA, type, n,
      ans = 0, left, right;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> Array[i];
  Array[0] = mod;
  Array[n + 1] = 0;
  for (int i = 1; i <= n + 1; i++) {
    if (Array[i] < Array[i - 1]) {
      V.push_back(make_pair(Array[prev], Array[i - 1]));
      prev = i;
    }
  }
  val = 1000;
  for (auto i : V) {
    val = (val / i.first) * i.second + val - (val / i.first) * i.first;
  }

  cout << val << endl;
  return 0;
}


Fixed submission:
/*input
7
100 130 130 130 115 115 150
*/

// #include<bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstdint>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;
long long mod = 1e9 + 7;
long long INF = 1e7;
double EPS = 1e-12;
const long long lim = 2e5 + 100;
typedef long long int lld;
typedef long long ll;

#define Error(x...)                                                            \
  {                                                                            \
    cout << "(" << #x << ")"                                                   \
         << " = ( ";                                                           \
    printIt(x);                                                                \
  }
template <typename T1> void printIt(T1 t1) { cout << t1 << " )" << endl; }
template <typename T1, typename... T2> void printIt(T1 t1, T2... t2) {
  cout << t1 << " , ";
  printIt(t2...);
}

#define popcount __builtin_popcountll

ll powP(ll x, ll y) {
  ll ans = 1, po = x % mod;
  while (y > 0) {
    if (y & 1)
      ans = ans * po % mod;
    y >>= 1;
    po = po * po % mod;
  }
  return ans;
}

vector<pair<ll, ll>> V;
ll Array[lim], C[lim];
vector<pair<ll, ll>> Allstocks;

int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);

  lld T, i, p, j, l, e, r, b, c, k, m, q, a, d, w, x, y, u, v, z, t, curr, prev,
      sum = 0, val, countA = 0, indicator = 0, pos, h, minA, maxA, type, n,
      ans = 0, left, right;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> Array[i];
  Array[0] = mod;
  Array[n + 1] = 0;
  prev = 0;
  for (int i = 1; i <= n + 1; i++) {
    if (Array[i] < Array[i - 1]) {
      V.push_back(make_pair(Array[prev], Array[i - 1]));
      prev = i;
    }
  }
  val = 1000;
  for (auto i : V) {
    val = (val / i.first) * i.second + val - (val / i.first) * i.first;
  }

  cout << val << endl;
  return 0;
}