This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
const ll Inf = 1e18;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, m;
cin >> n >> m;
vector <int> a(n), b(n), c(n), d(n);
for (int i = 0; i < n; ++i) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
}
vector <ll> dpL(n, Inf), dpR(n, Inf);
for (int i = 0; i < n; ++i) {
if (a[i] == 1) {
dpL[i] = d[i];
}
for (int j = i - 1; j >= 0; --j) {
if (a[i] <= c[j] && c[j] <= b[i]) {
dpL[i] = min(dpL[i], dpL[j] + d[i]);
}
}
}
for (int i = 0; i < n; ++i) {
if (b[i] == m) {
dpR[i] = d[i];
}
for (int j = i - 1; j >= 0; --j) {
if (a[i] <= c[j] && c[j] <= b[i]) {
dpR[i] = min(dpR[i], dpR[j] + d[i]);
}
}
}
ll ans = Inf;
for (int i = 0; i < n; ++i) {
ll costL = Inf, costR = Inf;
for (int j = i - 1; j >= 0; --j) {
if (a[i] <= c[j] && c[j] <= b[i]) {
costL = min(costL, dpL[j]);
costR = min(costR, dpR[j]);
}
}
if (a[i] == 1) costL = 0;
if (b[i] == m) costR = 0;
ans = min(ans, costL + costR + d[i]);
}
if (ans != Inf) cout << ans << '\n';
else cout << "-1\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... |