이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define in cin
#define out cout
#define int long long
using namespace std;
//ifstream in("in.in");
//ofstream out("out.out");
struct device {
    int l, r, hole, cost;
};
vector<device> devices;
const int nmax = 20, mmax = 1005, inf = (1LL << 60);
int n, m, dp[nmax][mmax][mmax];
void minSelf(int &a, int b) {
    if (b < a) {
        a = b;
    }
}
int ans = inf;
void check(int mask) {
    set<int> s;
    for (int i = 1; i <= m; i++) {
        s.insert(i);
    }
    int cost = 0;
    for (int i = 0; i < n; i++) {
        set<int> s2;
        if (mask & (1 << i)) cost += devices[i + 1].cost;
        for (auto k : s) {
            if ( (mask & (1 << i)) && (devices[i + 1].l <= k && k <= devices[i + 1].r) ) {
                    s2.insert(devices[i + 1].hole);
                }
            else {
                s2.insert(k);
            }
        }
        s = s2;
    }
    if (s.size() == 1) {
        ans = min(ans, cost);
    }
}
int32_t main() {
    in >> n >> m;
    if (m == 1) {
        out << "0\n";
        return 0;
    }
    devices.resize(n + 1);
    for (int i = 1; i <= n; i++) {
        int l, r, hole, cost;
        in >> l >> r >> hole >> cost;
        devices[i] = {l, r, hole, cost};
    }
    for (int mask = 0; mask < (1 << n); mask++) {
        check(mask);
    }
    if (ans == inf) {
        out << "-1\n";
    } else {
        out << ans << "\n";
    }
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |