Problem p00106 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define SZ(n) (int)n.size()
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
#define MP make_pair
#define PB push_back
const int INF = 1 << 28;
/*int n;
int dp[50][50][50];
int ta[]={380,550,850},wa[]={5,4,3};

int dfs(int a,int b,int c){
        //cout<<a<<" "<<b<<" "<<c<<endl;
        if(a*200+b*300+c*500>n)return dp[a][b][c]=INF;

        if(dp[a][b][c]!=INF)return dp[a][b][c];
        if(a*200+b*300+c*500==n){
                int ret=0;

                while(a>=5)a-=5,ret+=(380*5)*0.8;
                ret+=a*380;

                while(b>=4)b-=4,ret+=(550*4)*0.85;
                ret+=b*550;

                while(c>=3)c-=3,ret+=(850*3)*0.88;
                ret+=c*850;

                return dp[a][b][c]=ret;
        }

        int ret=INF;
ret=min( ret,dfs(a+1,b,c) );
ret=min( ret,dfs(a,b+1,c) );
ret=min( ret,dfs(a,b,c+1) );

return dp[a][b][c]=ret;
}*/

int main() {
  int n;
  while (cin >> n, n) {
    int ans = 1 << 20;
    for (int i = 0; i <= 50; i++) {
      for (int j = 0; j <= 50; j++) {
        for (int k = 0; k <= 50; k++) {
          int a = i, b = j, c = k, ret = 0;
          if (a * 200 + b * 300 + c * 500 == n) {

            while (a >= 5)
              a -= 5, ret += (380 * 5) * 0.8;
            ret += a * 380;

            while (b >= 4)
              b -= 4, ret += (550 * 4) * 0.85;
            ret += b * 550;

            while (c >= 3)
              c -= 3, ret += (850 * 3) * 0.88;
            ret += c * 850;
            ans = min(ans, ret);
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define SZ(n) (int)n.size()
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
#define MP make_pair
#define PB push_back

int n;
int dfs(int a, int b, int c) {
  // cout<<a<<" "<<b<<" "<<c<<endl;
  if (a * 200 + b * 300 + c * 500 > n)
    return 1 << 20;

  if (a * 200 + b * 300 + c * 500 == n) {
    int ret = 0;
    while (a >= 5)
      a -= 5, ret += (380 * 5) * 0.8;
    ret += a * 380;

    while (b >= 4)
      b -= 4, ret += (550 * 4) * 0.85;
    ret += b * 550;

    while (c >= 3)
      c -= 3, ret += (850 * 3) * 0.88;
    ret += c * 850;

    return ret;
  }

  int ret = 1 << 20;
  ret = min(ret, dfs(a + 1, b, c));
  ret = min(ret, dfs(a, b + 1, c));
  ret = min(ret, dfs(a, b, c + 1));

  return ret;
}

int main() {
  while (cin >> n, n) {
    cout << dfs(0, 0, 0) << endl;
  }
  return 0;
}