Problem p00874 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first1, BidirectionalIterator last1,
                      BidirectionalIterator first2,
                      BidirectionalIterator last2) {
  if ((first1 == last1) || (first2 == last2)) {
    return false;
  }
  BidirectionalIterator m1 = last1;
  BidirectionalIterator m2 = last2;
  --m2;
  while (--m1 != first1 && !(*m1 < *m2)) {
  }
  bool result = (m1 == first1) && !(*first1 < *m2);
  if (!result) {
    // ?
    while (first2 != m2 && !(*m1 < *first2)) {
      ++first2;
    }
    first1 = m1;
    std::iter_swap(first1, first2);
    ++first1;
    ++first2;
  }
  if ((first1 != last1) && (first2 != last2)) {
    // ?
    m1 = last1;
    m2 = first2;
    while ((m1 != first1) && (m2 != last2)) {
      std::iter_swap(--m1, m2);
      ++m2;
    }
    // ?
    std::reverse(first1, m1);
    std::reverse(first1, last1);
    std::reverse(m2, last2);
    std::reverse(first2, last2);
  }
  return !result;
}

template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first, BidirectionalIterator middle,
                      BidirectionalIterator last) {
  return next_combination(first, middle, middle, last);
}

int main() {
  while (1) {
    int w, d;
    cin >> w >> d;
    vector<int> ws;
    vector<int> ds;
    if (w >= d) {
      for (int i = 0; i < w; ++i) {
        int a;
        cin >> a;
        ws.push_back(a);
      }
      for (int i = 0; i < d; ++i) {
        int a;
        cin >> a;
        ds.push_back(a);
      }
    } else {
      for (int i = 0; i < w; ++i) {
        int a;
        cin >> a;
        ds.push_back(a);
      }
      for (int i = 0; i < d; ++i) {
        int a;
        cin >> a;
        ws.push_back(a);
      }
    }
    assert(ws.size() >= ds.size());

    vector<int> perm;
    for (int i = 0; i < ws.size(); ++i) {
      perm.push_back(i);
    }
    int finans = 500;

    do {
      int nans = 0;
      for (int i = 0; i < ds.size(); ++i) {
        if (ds[i] == ws[perm[i]]) {
          nans += ds[i];
        } else {
          nans += ds[i] + ws[perm[i]];
        }
      }
      for (int i = ds.size(); i < ws.size(); ++i) {
        nans += ws[perm[i]];
      }
      finans = min(finans, nans);

    } while (next_permutation(perm.begin(), perm.end()));
    cout << finans << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first1, BidirectionalIterator last1,
                      BidirectionalIterator first2,
                      BidirectionalIterator last2) {
  if ((first1 == last1) || (first2 == last2)) {
    return false;
  }
  BidirectionalIterator m1 = last1;
  BidirectionalIterator m2 = last2;
  --m2;
  while (--m1 != first1 && !(*m1 < *m2)) {
  }
  bool result = (m1 == first1) && !(*first1 < *m2);
  if (!result) {
    // ?
    while (first2 != m2 && !(*m1 < *first2)) {
      ++first2;
    }
    first1 = m1;
    std::iter_swap(first1, first2);
    ++first1;
    ++first2;
  }
  if ((first1 != last1) && (first2 != last2)) {
    // ?
    m1 = last1;
    m2 = first2;
    while ((m1 != first1) && (m2 != last2)) {
      std::iter_swap(--m1, m2);
      ++m2;
    }
    // ?
    std::reverse(first1, m1);
    std::reverse(first1, last1);
    std::reverse(m2, last2);
    std::reverse(first2, last2);
  }
  return !result;
}

template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first, BidirectionalIterator middle,
                      BidirectionalIterator last) {
  return next_combination(first, middle, middle, last);
}

int main() {
  while (1) {
    int w, d;
    cin >> w >> d;
    if (!w)
      break;
    vector<int> ws;
    vector<int> ds;
    if (w >= d) {
      for (int i = 0; i < w; ++i) {
        int a;
        cin >> a;
        ws.push_back(a);
      }
      for (int i = 0; i < d; ++i) {
        int a;
        cin >> a;
        ds.push_back(a);
      }
    } else {
      for (int i = 0; i < w; ++i) {
        int a;
        cin >> a;
        ds.push_back(a);
      }
      for (int i = 0; i < d; ++i) {
        int a;
        cin >> a;
        ws.push_back(a);
      }
    }
    assert(ws.size() >= ds.size());

    vector<int> perm;
    for (int i = 0; i < ws.size(); ++i) {
      perm.push_back(i);
    }
    int finans = 500;

    do {
      int nans = 0;
      for (int i = 0; i < ds.size(); ++i) {
        if (ds[i] == ws[perm[i]]) {
          nans += ds[i];
        } else {
          nans += ds[i] + ws[perm[i]];
        }
      }
      for (int i = ds.size(); i < ws.size(); ++i) {
        nans += ws[perm[i]];
      }
      finans = min(finans, nans);

    } while (next_permutation(perm.begin(), perm.end()));
    cout << finans << endl;
  }

  return 0;
}