#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define pi pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
template<class T> using upq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());}
template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());}
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define sumof(x) accumulate(all(x), 0ll)
#define dbg(x) "[" << #x " = " << (x) << "]"
#define el "\n"
using ll = long long;
using ld = long double;
template<class T> bool ckmx(T& a, const T b){return (a < b ? a = b, true : false);}
template<class T> bool ckmn(T& a, const T b){return (a > b ? a = b, true : false);}
const int N = 2e5 + 5;
const int MOD = 1e9 + 7;
const ll INF = numeric_limits<ll>::max() / 8;
/*
imagine we are at c[i]
[L[i], c[i]] and [c[i], R[i]] will become a "ladder"
so now we want to go out as far as possible from c[i] in 2 direction
minL[i], maxR[i], dp[i](0, 1)
the condition for j to be the next ladder for i is L[i] <= c[j] <= R[i] (j < i)
*/
int n, m, L[N], R[N], c[N];
ll dp[N][2], minL[N], maxR[N], W[N];
void Main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> L[i] >> R[i] >> c[i] >> W[i];
}
ll ans = INF;
for(int i = 1; i <= n; i++){
minL[i] = L[i], maxR[i] = R[i], dp[i][0] = dp[i][1] = W[i];
for(int j = i - 1; j >= 1; j--){
if(L[i] <= c[j] && c[j] <= R[i]){
if(ckmn(minL[i], minL[j])){
dp[i][0] = dp[j][0] + W[i];
}
else if(minL[i] == minL[j]) ckmn(dp[i][0], dp[j][0] + W[i]);
if(ckmx(maxR[i], maxR[j])){
dp[i][1] = dp[j][1] + W[i];
}
else if(maxR[i] == maxR[j]) ckmn(dp[i][1], dp[j][1] + W[i]);
}
}
if(minL[i] == 1 && maxR[i] == m){
ans = min(ans, dp[i][0] + dp[i][1] - W[i]);
}
}
cout << (ans == INF ? -1 : ans) << el;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
int t = 1; while(t--) Main();
return 0;
}
Compilation message (stderr)
pinball.cpp: In function 'int32_t main()':
pinball.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | freopen(name".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | freopen(name".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |