이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<cstdio>
#include<algorithm>
#include<utility>
#include<vector>
#define mp make_pair
using namespace std;
const int max_n = 100100;
typedef pair<int,int> pi;
typedef long long ll;
int n,l;
ll ans,dp1[max_n],dp2[max_n];
pi p[max_n];
vector<int> up[max_n], down[max_n];
void input() {
    scanf("%d%d",&n,&l);
    for(int i=0;i<n;i++) scanf("%d%d",&p[i].first,&p[i].second);
    for(int i=0;i<n;i++) {
        up[p[i].first].push_back(i);
        down[p[i].second].push_back(i);
    }
    return;
}
inline int abs(int x) {return x>0?x:-x;}
ll fill1(int x);
ll fill2(int x);
ll fill1(int x) {
    if(dp1[x]) return dp1[x];
    ll ret = 0;
    for(int i=0; i<(int)down[p[x].second].size(); i++) {
        int nxt = down[p[x].second][i];
        if(p[x].first >= p[nxt].first) continue;
        ret = max(ret, fill2(nxt));
    }
    return dp1[x] = ret + 1LL*abs(p[x].first - p[x].second) + l;
}
ll fill2(int x) {
    if(dp2[x]) return dp2[x];
    ll ret = 0;
    for(int i=0; i<(int)up[p[x].first].size(); i++) {
        int nxt = up[p[x].first][i];
        if(p[x].second >= p[nxt].second) continue;
        ret = max(ret, fill1(nxt));
    }
    return dp2[x] = ret + 1LL*abs(p[x].first - p[x].second) + l;
}
void solve() {
    for(int i=0;i<n;i++) fill1(i);
    for(int i=0;i<n;i++) fill2(i);
    for(int i=0;i<n;i++) {
        ans = max(ans, dp1[i]);
        ans = max(ans, dp2[i]);
    }
    printf("%lld",ans);
    return;
}
int main() {
    input();
    solve();
    return 0;
}
| # | 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... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |