Problem p03370 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF (int)1e9
#define MOD 1000000007
#define tr(c, it)                                                              \
  for (typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz size
#define ll long long
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define MEM(a, b) memset(a, (b), sizeof(a))
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element)                                           \
  (find(all(container), element) != container.end())
#define FOR(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define PRE(i, m, n, in) for (int(i) = (m); (i) < (n); i += in)
#define RFOR(i, n) for (int i = n; i >= 0; i--)
#define RPRE(i, j, k, in) for (int i = j; i >= k; i -= in)
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert(B <= A && A <= C)
using namespace std;
typedef pair<int, int> II;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<II> VII;
typedef vector<VI> VVI;
typedef vector<VII> VVII;
typedef map<int, int> MPII;
typedef set<int> SETI;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  freopen("input.txt", "r", stdin);
  int N, X;
  cin >> N >> X;
  VI m(N);
  int res = N;
  int sum = 0;
  FOR(i, N) {
    cin >> m[i];
    sum += m[i];
  }
  sort(all(m));
  int t = X - sum;
  int i = 0;
  while (t > 0 && i < N) {
    // cout << t/m[i] << endl;
    res += t / m[i];
    t = t - (t / m[i]) * m[i];
    i++;
  }
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF (int)1e9
#define MOD 1000000007
#define tr(c, it)                                                              \
  for (typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz size
#define ll long long
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define MEM(a, b) memset(a, (b), sizeof(a))
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element)                                           \
  (find(all(container), element) != container.end())
#define FOR(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define PRE(i, m, n, in) for (int(i) = (m); (i) < (n); i += in)
#define RFOR(i, n) for (int i = n; i >= 0; i--)
#define RPRE(i, j, k, in) for (int i = j; i >= k; i -= in)
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert(B <= A && A <= C)
using namespace std;
typedef pair<int, int> II;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<II> VII;
typedef vector<VI> VVI;
typedef vector<VII> VVII;
typedef map<int, int> MPII;
typedef set<int> SETI;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // freopen("input.txt","r",stdin);
  int N, X;
  cin >> N >> X;
  VI m(N);
  int res = N;
  int sum = 0;
  FOR(i, N) {
    cin >> m[i];
    sum += m[i];
  }
  sort(all(m));
  int t = X - sum;
  int i = 0;
  while (t > 0 && i < N) {
    // cout << t/m[i] << endl;
    res += t / m[i];
    t = t - (t / m[i]) * m[i];
    i++;
  }
  cout << res << endl;
  return 0;
}