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

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

// Ï
int p;
int n1, n5, n10, n50, n100, n500;

// ¨ÂèÌ
int cntMaisu(int money) {
  int cnt = 0;
  while (money != 0) {
    if (money >= 500) {
      cnt += (money / 500);
      money -= 500 * (money / 500);
    } else if (money >= 100) {
      cnt += (money / 100);
      money -= 100 * (money / 100);
    } else if (money >= 50) {
      cnt += (money / 50);
      money -= 50 * (money / 50);
    } else if (money >= 10) {
      cnt += (money / 10);
      money -= 10 * (money / 10);
    } else if (money >= 5) {
      cnt += (money / 5);
      money -= 5 * (money / 5);
    } else if (money >= 1) {
      cnt += (money);
      money -= (money);
    }
  }
  return cnt;
}
// ^¦çê½ÅÌÅ¬Ìx¥¢û
int calc(int money) {
  int nn1 = n1;
  int nn5 = n5;
  int nn10 = n10;
  int nn50 = n50;
  int nn100 = n100;
  int nn500 = n500;

  int cnt = 0;
  while (money != 0) {
    if (money >= 500 && nn500 >= 1) {
      int t = min(money / 500, nn500);
      cnt += t;
      money -= t * 500;
      nn500 -= t;
    } else if (money >= 100 && nn100 >= 1) {
      int t = min(money / 100, nn100);
      cnt += t;
      money -= t * 100;
      nn100 -= t;
    } else if (money >= 50 && nn50 >= 1) {
      int t = min(money / 50, nn50);
      cnt += t;
      money -= t * 50;
      nn50 -= t;
    } else if (money >= 10 && nn10 >= 1) {
      int t = min(money / 10, nn10);
      cnt += t;
      money -= t * 10;
      nn10 -= t;
    } else if (money >= 5 && nn5 >= 1) {
      int t = min(money / 5, nn5);
      cnt += t;
      money -= t * 5;
      nn5 -= t;
    } else if (money >= 1 && nn1 >= 1) {
      int t = min(money, nn1);
      cnt += t;
      money -= t;
      nn1 -= t;
    } else
      return -1;
  }
  return cnt;
}

int main() {
  while (cin >> p >> n1 >> n5 >> n10 >> n50 >> n100 >> n500 && !(p == 0)) {
    int minCost = 100000000;
    // ¿å¤ÇÌàzÅ¥¤
    int ret = calc(p);
    if (ret != -1)
      minCost = min(minCost, ret);
    // ã©ç©Ô¹é
    // for(int i = 5; i <= 1000000; i+=5){
    //	int p1=p-(p%i)+i;
    //	int r=calc(p1);
    //	if(r!=-1)
    for (int i = p; i <= p + 1000; i++) {
      int p1 = p - (p % i) + i;
      int r = calc(p1);
      if (r != -1)
        minCost = min(minCost, r + cntMaisu(p1 - p));
    }
    //		}
    cout << minCost << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int cntMaisu(int money) {
  int cnt = 0;
  while (money != 0) {
    if (money >= 500) {
      cnt++;
      money -= 500;
    } else if (money >= 100) {
      cnt++;
      money -= 100;
    } else if (money >= 50) {
      cnt++;
      money -= 50;
    } else if (money >= 10) {
      cnt++;
      money -= 10;
    } else if (money >= 5) {
      cnt++;
      money -= 5;
    } else if (money >= 1) {
      cnt++;
      money--;
    }
  }
  return cnt;
}

int p;
int n1, n5, n10, n50, n100, n500;

// ^¦çê½ÅÌÅ¬Ìx¥¢û
int calc(int money) {
  int nn1 = n1;
  int nn5 = n5;
  int nn10 = n10;
  int nn50 = n50;
  int nn100 = n100;
  int nn500 = n500;

  int cnt = 0;
  while (money != 0) {
    if (money >= 500 && nn500 >= 1) {
      cnt++;
      money -= 500;
      nn500--;
    } else if (money >= 100 && nn100 >= 1) {
      cnt++;
      money -= 100;
      nn100--;
    } else if (money >= 50 && nn50 >= 1) {
      cnt++;
      money -= 50;
      nn50--;
    } else if (money >= 10 && nn10 >= 1) {
      cnt++;
      money -= 10;
      nn10--;
    } else if (money >= 5 && nn5 >= 1) {
      cnt++;
      money -= 5;
      nn5--;
    } else if (money >= 1 && nn1 >= 1) {
      cnt++;
      money--;
      nn1--;
    } else
      return -1;
  }
  return cnt;
}

int main() {

  while (cin >> p >> n1 >> n5 >> n10 >> n50 >> n100 >> n500 && !(p == 0)) {
    int minCost = 100000000;
    int cnt = min(n500, p / 500);
    p -= cnt * 500;
    n500 -= cnt;
    int sum = cnt;

    // ¿å¤ÇÌàzÅ¥¤
    int ret = calc(p);
    if (ret != -1)
      minCost = min(minCost, ret);
    // for(int j = 2; j <= 1000000; j*=2){
    //	int p1=p-(p%(j))+j;
    //	int r=calc(p1);
    //	if(r!=-1)
    //		minCost=min(minCost,r+cntMaisu(p1-p));
    // }
    //  ã©ç©Ô¹é
    for (int i = 5; i <= 166000; i += 5) {
      int p1 = p - (p % (i)) + i;
      int r = calc(p1);
      if (r != -1)
        minCost = min(minCost, r + cntMaisu(p1 - p));
      // i*=2;
      // int p2=p-(p%i)+i;
      // int r2=calc(p2);
      // if(r2!=-1)
      //	minCost=min(minCost,r2+cntMaisu(p2-p));
    }
    cout << minCost + sum << endl;
  }

  return 0;
}