Problem p02603 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;

  long long int ans = 1000;
  long long int counter = 1000;
  long long int a[n];

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (int i = 0; i < n - 1; i++) {
    if (a[i] < a[i + 1]) {
      counter = ans / a[i];
      ans -= (counter)*a[i];
      ans += counter * a[i + 1];
    }
  }

  cout << ans;
}


Error: -8

Fixed submission:

#include <bits/stdc++.h>
#include <cstdlib>

/*If you are going to try,keep going until the end ,or don't even try to start*/

/* Remember, Hope is a good thing... May be the best of things... And No Good
 * thing ever Dies !!!*/

using namespace std;

#define nada '\\n'
#define st string
#define S second
#define F first
#define MP make_pair
#define ll long long
#define pb push_back
#define ld long double
#define isnttriangle(a, b, c) (a + b <= c || a + c <= b || b + c <= a)
#define todegree(rad) rad * (180.0 / pi)
#define rrp(i, a, b) for (ll i = a; i >= b; i--)
#define repp(i, a, b) for (int i = int(a); i <= int(b); i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define allr(v) ((v).rbegin()), ((v).rend())
#define all(v) ((v).begin()), ((v).end())
#define fx(n) cout << fixed << setprecision(n);
#define sz(v) ((int)(v).size())
#define Test                                                                   \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define mod 1000000009LL
#define EPS 1e-15

const double ME = 2.71828;
const int N = 1e7 + 20;
const int inf = 0x3f3f3f3f;
const double pi = 3.141592653;

void LOSER() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

void file() {
  freopen("records.in", "r", stdin);
  // freopen("text.out", "w", stdout);
}

ll gcd(ll a, ll b) {
  // Eculidean Algorthim
  return (!b) ? a : gcd(b, a % b);
  // complixty=log(ab);
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll Fib_recur(ll n) {
  vector<ll> v(n + 5, 0);
  // BaseCase
  v[1] = 0;
  v[2] = 1;
  repp(i, 3, n) v[i] = v[i - 1] + v[i - 2];
  return v[n];
}

bool isprime(ll n) {
  if (n == 1)
    return 0;
  for (ll i = 2; i <= sqrt(n); i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
/*   //SEIVE//

ve
ctor<int>seive(1e7,1);
  //  fill(all(seive),1);
   seive[0]=seive[1]=0;
   for(int i=2; i*i<=1e7; i++)
   {
       for(int j=i*i; j<=1e7; j+=i)
           seive[j]=0;

   }*/
bool comp(pair<ll, ll> &a, pair<ll, ll> &b) {
  if (a.F < b.F)
    return true;
  else if (a.F == b.F && a.S >= b.S)
    return true;
  else
    return false;
}
ll sumNatural(ll n) {
  ll sum = (n * (n + 1)) / 2;
  return sum;
}
ll sumNaturaleven(ll n) {
  ll sum = (n * (n + 1));
  return sum;
}
ll suminRange(ll l, ll r) { return sumNatural(r) - sumNatural(l - 1); }
ll sumEven(ll l, ll r) {
  return sumNaturaleven(r / 2) - sumNaturaleven((l - 1) / 2);
}
//  vector<ll>t (all(s));
st binary(ll n) {
  st s = "";
  bool neg = 0;
  if (n < 0) {
    n *= -1;
    neg = 1;
  }
  while (n > 0) {
    if (n % 2 == 0)
      s += '0';
    else
      s += '1';
    n /= 2;
  }
  reverse(all(s));
  if (neg) {
    s = '1' + s;
  }
  return s;
}

void Solve() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (auto &i : v)
    cin >> i;
  sort(all(v));
  int res = 1000;
  res /= v[0];
  cout << res * v[n - 1];
}

int main() {
  // fx(6)
  // file();
  LOSER();
  Test { Solve(); }
}