Problem p03601 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define si(X) scanf("%d", &(X))
#define sll(X) scanf("%lld", &(X))
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int mod = 1e9 + 7;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll expo(ll base, ll pow) {
  ll ans = 1;
  while (pow != 0) {
    if (pow & 1 == 1) {
      ans = ans * base;
      ans = ans % mod;
    }
    base *= base;
    base %= mod;
    pow /= 2;
  }
  return ans;
}
ll inv(ll x) { return expo(x, mod - 2); }
ull llrand() {
  ull r = 0;
  for (int i = 0; i < 5; ++i) {
    r = (r << 15) | (rand() & 0x7FFF);
  }
  return r & 0xFFFFFFFFFFFFFFFFULL;
}
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; // last 4 diagonal
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
/*---------------------------------------------------------------------------------------------------------------------*/

const int M = 100001;

int main() {
  // freopen("input.txt", "rt", stdin);
  // freopen("output.txt", "wt", stdout);

  int a, b, c, d, e, f;

  cin >> a >> b >> c >> d >> e >> f;

  double ans = 0.0;
  int one = 0, two = 0;

  for (int i1 = 0; i1 <= 500; i1++) {
    if (i1 * a * 100 > f)
      break;
    for (int i2 = 0; i2 <= 500; i2++) {
      if (100 * (i1 * a + i2 * b) > f)
        continue;
      for (int i3 = 0; i3 <= 500; i3++) {

        int water = 100 * (i1 * a + i2 * b);
        int sugar = i3 * c;

        if (e * (i1 * a + i2 * b) < sugar)
          continue;
        if (sugar + water > f)
          continue;

        for (int i4 = 0; i4 <= 500; i4++) {

          water = 100 * (i1 * a + i2 * b);
          sugar = i3 * c + i4 * d;
          if (i1 == 1 && i3 == 1 && i2 + i4 == 0) {
            // cout<<water<<"  "<<sugar<<endl;
          }
          if (e * (i1 * a + i2 * b) < sugar)
            continue;
          if (sugar + water > f)
            continue;

          double density = (sugar * 1.0) / (sugar * 1.0 + water * 1.0);
          if (density > ans) {
            // cout<<density<<"  "<<ans<<"  "<<water<<"  "<<sugar<<endl;
            ans = density;
            one = water + sugar;
            two = sugar;
          }
        }
      }
    }
  }

  if (ans == 0) {
    while (1) {
    }
  }
  cout << one << " " << two;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define si(X) scanf("%d", &(X))
#define sll(X) scanf("%lld", &(X))
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int mod = 1e9 + 7;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll expo(ll base, ll pow) {
  ll ans = 1;
  while (pow != 0) {
    if (pow & 1 == 1) {
      ans = ans * base;
      ans = ans % mod;
    }
    base *= base;
    base %= mod;
    pow /= 2;
  }
  return ans;
}
ll inv(ll x) { return expo(x, mod - 2); }
ull llrand() {
  ull r = 0;
  for (int i = 0; i < 5; ++i) {
    r = (r << 15) | (rand() & 0x7FFF);
  }
  return r & 0xFFFFFFFFFFFFFFFFULL;
}
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; // last 4 diagonal
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
/*---------------------------------------------------------------------------------------------------------------------*/

const int M = 100001;

int main() {
  // freopen("input.txt", "rt", stdin);
  // freopen("output.txt", "wt", stdout);

  int a, b, c, d, e, f;

  cin >> a >> b >> c >> d >> e >> f;

  double ans = 0.0;
  int one = 0, two = 0;

  for (int i1 = 0; i1 <= 500; i1++) {
    if (i1 * a * 100 > f)
      break;
    for (int i2 = 0; i2 <= 500; i2++) {
      if (100 * (i1 * a + i2 * b) > f)
        continue;
      for (int i3 = 0; i3 <= 500; i3++) {

        int water = 100 * (i1 * a + i2 * b);
        int sugar = i3 * c;

        if (e * (i1 * a + i2 * b) < sugar)
          continue;
        if (sugar + water > f)
          continue;

        for (int i4 = 0; i4 <= 500; i4++) {

          water = 100 * (i1 * a + i2 * b);
          sugar = i3 * c + i4 * d;
          if (i1 == 1 && i3 == 1 && i2 + i4 == 0) {
            // cout<<water<<"  "<<sugar<<endl;
          }
          if (e * (i1 * a + i2 * b) < sugar)
            continue;
          if (sugar + water > f)
            continue;

          double density = (sugar * 1.0) / (sugar * 1.0 + water * 1.0);
          if (density > ans) {
            // cout<<density<<"  "<<ans<<"  "<<water<<"  "<<sugar<<endl;
            ans = density;
            one = water + sugar;
            two = sugar;
          }
        }
      }
    }
  }

  if (ans == 0) {
    one = 100 * (min(a, b));
    two = 0;
  }
  cout << one << " " << two;
}