#include <bits/stdc++.h>
using namespace std;
const int nx=5e3+5;
#define ll long long
struct plan
{
ll l, r, t, c, s;
bool operator<(const plan &o) const {return s<o.s;}
} p[nx];
ll n, m, dp[nx], res=1e18;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m;
for (int i=1; i<=m; i++)
{
cin>>p[i].t>>p[i].l>>p[i].r>>p[i].c;
p[i].s=(p[i].r==n)?1e18:p[i].r+p[i].t;
}
sort(p+1, p+m+1);
for (int i=1; i<=m; i++)
{
dp[i]=1e18;
if (p[i].l==1) dp[i]=p[i].c;
else
{
for (int j=1; j<i; j++)
{
if (p[j].s<p[i].s)
{
if ((((p[j].r+p[j].t)-(p[i].l-p[i].t)+1)/2)>=max(p[j].t, p[i].t))
{
dp[i]=min(dp[i], dp[j]+p[i].c);
}
}
}
}
}
/*
for (int i=1; i<=m; i++)
{
cout<<"debug "<<p[i].l<<' '<<p[i].r<<' '<<p[i].t<<' '<<p[i].c<<'\n';
cout<<"cost "<<dp[i]<<'\n';
}
*/
for (int i=1; i<=m; i++)
{
if (p[i].r==n)
{
res=min(res, dp[i]);
}
}
if (res==1e18) cout<<-1;
else cout<<res;
}
# | 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... |