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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  int array[16];
  int state, sum_cost, used_num;
};

int N, M;
int POW[17];
int dp[65536];
int futon[16], demand[101];

void func() {

  futon[0] = 0;
  for (int i = 1; i <= N; i++)
    scanf("%d", &futon[i]);
  demand[0] = 0;
  for (int i = 1; i <= M; i++)
    scanf("%d", &demand[i]);

  sort(demand, demand + (M + 1));

  N++;

  for (int i = 0; i < POW[N]; i++)
    dp[i] = BIG_NUM;

  Info first;
  first.state = 0;
  first.used_num = 0;
  first.sum_cost = 0;

  queue<Info> Q;

  Q.push(first);

  int tmp_sum, next_state;

  while (!Q.empty()) {

    if (Q.front().used_num == N) {
      Q.pop();
    } else if (Q.front().sum_cost > dp[Q.front().state]) {
      Q.pop();
    } else {

      for (int loop = 0; loop < N; loop++) {
        if (Q.front().state & (1 << loop)) {
          // Do noghint
        } else {
          Info next_info;
          for (int i = 0; i < Q.front().used_num; i++)
            next_info.array[i] = Q.front().array[i];
          next_info.array[Q.front().used_num] = futon[loop];

          if (Q.front().used_num > 0) {
            next_info.array[Q.front().used_num] +=
                next_info.array[Q.front().used_num - 1];
          }
          int index = 0;
          tmp_sum = 0;
          for (int i = 1; i <= M; i++) {
            while (index < Q.front().used_num &&
                   abs(demand[i] - next_info.array[index]) >
                       abs(demand[i] - next_info.array[index + 1]))
              index++;
            tmp_sum += abs(demand[i] - next_info.array[index]);
          }

          next_state = Q.front().state + POW[loop];

          if (dp[next_state] > tmp_sum) {
            dp[next_state] = tmp_sum;
            next_info.state = next_state;
            next_info.sum_cost = tmp_sum;
            next_info.used_num = Q.front().used_num + 1;
            Q.push(next_info);
          }
        }
      }
      Q.pop();
    }
  }

  printf("%d\\n", dp[POW[N] - 1]);
}

int main() {

  for (int i = 0; i < 17; i++)
    POW[i] = pow(2, i);

  while (true) {
    scanf("%d %d", &N, &M);
    if (N == 0 && M == 0)
      break;

    func();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  int array[16];
  int state, sum_cost, used_num;
};

int N, M;
int POW[17];
int dp[32768];
int futon[16], demand[101];

void func() {

  futon[0] = 0;
  for (int i = 1; i <= N; i++)
    scanf("%d", &futon[i]);
  demand[0] = 0;
  for (int i = 1; i <= M; i++)
    scanf("%d", &demand[i]);

  sort(demand, demand + (M + 1));

  N++;

  for (int i = 0; i < POW[N]; i++)
    dp[i] = BIG_NUM;

  Info first;
  first.state = 0;
  first.used_num = 0;
  first.sum_cost = 0;

  queue<Info> Q;

  Q.push(first);

  int tmp_sum, next_state;

  while (!Q.empty()) {

    if (Q.front().used_num == N) {
      Q.pop();
    } else if (Q.front().sum_cost > dp[Q.front().state]) {
      Q.pop();
    } else {

      for (int loop = 0; loop < N; loop++) {
        if (Q.front().state & (1 << loop)) {
          // Do noghint
        } else {
          Info next_info;
          for (int i = 0; i < Q.front().used_num; i++)
            next_info.array[i] = Q.front().array[i];
          next_info.array[Q.front().used_num] = futon[loop];

          if (Q.front().used_num > 0) {
            next_info.array[Q.front().used_num] +=
                next_info.array[Q.front().used_num - 1];
          }
          int index = 0;
          tmp_sum = 0;
          for (int i = 1; i <= M; i++) {
            while (index < Q.front().used_num &&
                   abs(demand[i] - next_info.array[index]) >
                       abs(demand[i] - next_info.array[index + 1]))
              index++;
            tmp_sum += abs(demand[i] - next_info.array[index]);
          }

          next_state = Q.front().state + POW[loop];

          if (dp[next_state] > tmp_sum) {
            dp[next_state] = tmp_sum;
            next_info.state = next_state;
            next_info.sum_cost = tmp_sum;
            next_info.used_num = Q.front().used_num + 1;
            Q.push(next_info);
          }
        }
      }
      Q.pop();
    }
  }

  printf("%d\\n", dp[POW[N] - 1]);
}

int main() {

  for (int i = 0; i < 17; i++)
    POW[i] = pow(2, i);

  while (true) {
    scanf("%d %d", &N, &M);
    if (N == 0 && M == 0)
      break;

    func();
  }
  return 0;
}