Problem:
F - ７歳教
７歳の誕生日に"彼"は父から告げられた．
「お前が７歳になったということは，私はもう引退だ．今日からお前が７歳教の長だ．
７歳教の戒律により，７歳教の長が７歳の間だけ布教活動を行うことが認められている．
この宇宙船を使って，よりたくさんの人々に７歳教の素晴らしさを広めてきなさい．」
７歳教をよりたくさんの人に広めるために"彼"は，自分の周りの星についての情報を集めた．
"彼"の星の周りには，
n
個の惑星があり，惑星は
1, ... , n
で番号付けされていることがわかった．
惑星
i
から 惑星
j
の間を移動するには，
w_i_j
(年)の時間がかかることもわかった．
そして，それぞれの星で彼が７歳でいられる時間，つまり布教活動できる時間が異なることがわかった．
それぞれの惑星上では，宇宙暦
l_i
年1月1日00:00:00以降
r_i
年1月1日00:00:00より前までの時間帯であれば7歳でいられる．
たとえ一度ある星で７歳以外の年齢になっても，その星で後から７歳になった場合や，別の星で７歳になれば布教活動を再開することができる．
しかしどの星でどれくらい，そしてどの順序で布教活動を行えばより多くの人に７歳教を伝えることができるのか，
"彼"にはわからなかった．そこで"彼"はプログラミングが得意なあなたに助けを求めることにした．
現在は宇宙暦0年1月1日00:00:00であり，"彼"は惑星
s
にいる．
惑星間を最適に移動・滞在した場合に布教活動が行える最長の年数，
つまり"彼"が７歳でかつ惑星で過ごす最長の年数
T
を求めてあげよう．
ただし，年数
T
には移動時間は含まれない．
入力形式
入力は以下の形式で与えられる．
n
s
l_1
r_1
...
l_{n}
r_{n}
w_{1,1}
w_{1,2}
...
w_{1,n}
...
w_{n,1}
w_{n,2}
...
w_{n,n}
n
は惑星の個数である．
s
は主人公が最初にいる惑星をあらわす．
l_i
と
r_i
はそれぞれ，惑星
i
での7歳でいられる時間帯の下限と上限をあらわす．
w_i_j
は，惑星
i
から惑星
j
を移動するのにかかる時間をあらわす．
入力はすべて整数である．
出力形式
答え
T
を出力せよ．
制約
1 ≤ n ≤ 500
1 ≤ s ≤ n
0 ≤ l_i < r_i ≤ 10^8
0 ≤ w_{ij} ≤ 10^8
(
i
≠
j
)
w_{ij} = 0
(
i
=
j
)
入出力例
入力例 1
2 1
0 100
150 250
0 100
100 0
出力例 1
150
入力例 2
5 1
7 44
10 49
38 48
11 23
11 30
0 1 7 2 7
10 0 3 8 10
4 8 0 2 5
3 2 1 0 4
8 4 3 3 0
出力例 2
41

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=int64_t;

int main(){
  int n,s;
  cin >> n >> s;
  s--;
  
  vector<pair<ll,ll>> lr(n);
  vector w(n,vector<ll>(n,0));
  ll l,r;
  for(int i=0;i<n;i++){
    cin >> l >> r;
    lr[i]={l,r};
  }
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++) cin >> w[i][j];
  }
  
  if(n==1){
    cout << lr[0].second-lr[0].first << endl;
    return 0;
  }
  
  for(int k=0;k<n;k++){
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        w[i][j]=min(w[i][j],w[i][k]+w[k][j]);
       }
    }
  }
 /* for(int i=0;i<n;i++){
    for(int j=0;j<n;j++) cout << w[i][j] << " ";
    cout << endl;
  }*/

  vector dp(n+1,vector<ll>(n,-1));
  ll ans=0;
  for(int i=0;i<n;i++){
    dp[0][i]=lr[i].second-max(w[s][i],lr[i].first);
    ans=max(ans,dp[0][i]);
  }
  
  for(int i=1;i<n;i++){
    for(int j=0;j<n;j++){
      if(dp[i-1][j]<0) continue;
      for(int k=0;k<n;k++){
        if(j==k) continue;
        ll tm=lr[k].second-max(lr[j].second+w[j][k],lr[k].first);
        if(tm<=0) continue;
        dp[i][k]=max(dp[i][k],dp[i-1][j]+tm);
        ans=max(ans,dp[i][k]);
      }
    }
  }
  
  cout << ans << endl;
}
accuracy: 1
time_ms: 220
memory_kb: 7396
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=int64_t;

int main(){
  int n,s;
  cin >> n >> s;
  s--;
  
  vector<tuple<ll,ll,int>> lr(n),lro(n);
  vector w(n,vector<ll>(n,0));
  ll l,r,rmx=0;;
  for(int i=0;i<n;i++){
    cin >> l >> r;
    lr[i]={l,r,i};
    rmx=max(rmx,r);
  }
  lro=lr;
  //cout << rmx << endl;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++) cin >> w[i][j];
  }
  
  if(n==1){
    cout << get<1>(lr[0])-get<0>(lr[0]) << endl;
    return 0;
  }
  
  for(int k=0;k<n;k++){
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        w[i][j]=min(w[i][j],w[i][k]+w[k][j]);
       }
    }
  }
 /* for(int i=0;i<n;i++){
    for(int j=0;j<n;j++) cout << w[i][j] << " ";
    cout << endl;
  }*/

  int si;
  sort(lr.begin(),lr.end());
  for(int i=0;i<n;i++){
    if(get<2>(lr[i])==s){
      si=i;
      break;
    }
  }
  

  
  vector<ll> tim(n,0);
  ll ans=0;
  priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> pq;
  pq.push({0LL,si});
  while(!pq.empty()){
    auto [t,ci]=pq.top();
    //cout << t << endl;
    pq.pop();
    int ii=get<2>(lr[ci]);
    ll tmc=0;
    if(t!=0) tmc=tim[ii];
    vector<pair<ll,int>> fr;
    for(int i=0;i<n;i++){
      if((t!=0)&&(ci==i)) continue;
      int iin=get<2>(lr[i]);
      if(t+w[ii][iin]>=get<1>(lr[i])) continue;
      int flg=0;
      for(int j=0;j<(int)fr.size();j++){
        auto [tp,ip]=fr[j];
        if(tp+w[ip][iin]<=get<0>(lr[i])){
          flg=1;
          break;
        }
      }
      if(flg) break;
      ll tm=tmc+get<1>(lr[i])-max(get<0>(lr[i]),t+w[ii][iin]);
      //cout << tmc << " " << get<1>(lr[i]) << " " << get<0>(lr[i]) << " " << t+w[ii][iin] << endl;
      if(tim[iin]<tm){
        tim[iin]=tm;
        ans=max(ans,tm);
        pq.push({get<1>(lr[i]),i});
        fr.emplace_back(get<1>(lr[i]),iin);
      }
    }
  }
  cout << ans << endl;
}
accuracy: 0.7941176470588235
time_ms: 160
memory_kb: 6132
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=int64_t;

int main(){
  int n,s;
  cin >> n >> s;
  s--;
  
  vector<tuple<ll,ll,int>> lr(n),lro(n);
  vector w(n,vector<ll>(n,0));
  ll l,r,rmx=0;;
  for(int i=0;i<n;i++){
    cin >> l >> r;
    lr[i]={l,r,i};
    rmx=max(rmx,r);
  }
  lro=lr;
  //cout << rmx << endl;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++) cin >> w[i][j];
  }
  
  if(n==1){
    cout << get<1>(lr[0])-get<0>(lr[0]) << endl;
    return 0;
  }
  
  int flg=1;
  while(flg==1){
    flg=0;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        if(j==i) continue;
        for(int k=0;k<n;k++){
          if((k==i)||(k==j)) continue;
          ll tt=w[i][k]+w[k][j];
          if(w[i][j]>tt){
            flg=1;
            w[i][j]=tt;
          }
        }
      }
    }
  }
 /* for(int i=0;i<n;i++){
    for(int j=0;j<n;j++) cout << w[i][j] << " ";
    cout << endl;
  }*/

  int si;
  sort(lr.begin(),lr.end());
  for(int i=0;i<n;i++){
    if(get<2>(lr[i])==s){
      si=i;
      break;
    }
  }
  

  
  vector<ll> tim(n,0);
  ll ans=0;
  priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> pq;
  pq.push({0LL,si});
  while(!pq.empty()){
    auto [t,ci]=pq.top();
    //cout << t << endl;
    pq.pop();
    int ii=get<2>(lr[ci]);
    ll tmc=0;
    if(t!=0) tmc=tim[ii];
    vector<pair<ll,int>> fr;
    for(int i=0;i<n;i++){
      if((t!=0)&&(ci==i)) continue;
      int iin=get<2>(lr[i]);
      if(t+w[ii][iin]>=get<1>(lr[i])) continue;
      flg=0;
      for(int j=0;j<(int)fr.size();j++){
        auto [tp,ip]=fr[j];
        if(tp+w[ip][iin]<=get<0>(lr[i])){
          flg=1;
          break;
        }
      }
      if(flg) break;
      ll tm=tmc+get<1>(lr[i])-max(get<0>(lr[i]),t+w[ii][iin]);
      //cout << tmc << " " << get<1>(lr[i]) << " " << get<0>(lr[i]) << " " << t+w[ii][iin] << endl;
      if(tim[iin]<tm){
        tim[iin]=tm;
        ans=max(ans,tm);
        pq.push({get<1>(lr[i]),i});
        fr.emplace_back(get<1>(lr[i]),iin);
      }
    }
  }
  cout << ans << endl;
}
accuracy: 0.7941176470588235
time_ms: 620
memory_kb: 5972
final_rank: 10

code_snippet: #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int N, S, L[509], R[509], p[509], W[509][509], dp[509];
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	cin >> N >> S; --S;
	for (int i = 0; i < N; ++i) cin >> L[i] >> R[i], p[i] = i;
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N; ++j) {
			cin >> W[i][j];
		}
		dp[i] = -(1 << 30);
	}
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N; ++j) {
			for (int k = 0; k < N; ++k) {
				W[j][k] = min(W[j][k], W[j][i] + W[i][k]);
			}
		}
	}
	sort(p, p + N, [&](int i, int j) { return R[i] < R[j]; });
	for (int i = 0; i < N; ++i) {
		int v = p[i];
		int reach = max(L[v], W[S][v]);
		if (reach <= R[v]) dp[v] = R[v] - reach;
		for (int j = 0; j < N; ++j) {
			if (j == v) continue;
			int reachx = max(L[v], R[j] + W[j][v]);
			if (reachx <= R[v]) dp[v] = max(dp[v], dp[j] + R[v] - reachx);
		}
	}
	cout << *max_element(dp, dp + N) << endl;
	return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 4224
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

using pi = pair<int,int>;
using P = pair<pi,int>;

int main(){
    int n,s;
    cin >>n >>s;
    --s;

    vector<P> v(n);
    rep(i,n){
        int l,r;
        cin >>l >>r;
        v[i] = {{r,l},i};
    }
    sort(all(v));

    vector<vector<ll>> w(n,vector<ll>(n));
    rep(i,n)rep(j,n) cin >>w[i][j];

    rep(k,n)rep(i,n)rep(j,n) w[i][j] = min(w[i][j], w[i][k]+w[k][j]);

    const ll INF = LLONG_MAX/3;
    vector<ll> dp(n,-INF);
    rep(i,n){
        int to = v[i].se;
        ll d = w[s][to];
        ll l = v[i].fi.se, r = v[i].fi.fi;

        ll a = r-max(l,d);
        dp[i] = max(dp[i],a);
    }

    rep(i,n){
        int u = v[i].se;
        ll R = v[i].fi.fi;

        for(int j=i+1; j<n; ++j){
            int to = v[j].se;
            ll d = w[u][to];
            ll l = v[j].fi.se, r = v[j].fi.fi;

            ll a = r-max(l,R+d);
            if(a>=0) dp[j] = max(dp[j], dp[i]+a);
        }
    }

    ll ans = 0;
    rep(i,n) ans = max(ans, dp[i]);
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 5000
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

using pi = pair<int,int>;
using P = pair<pi,int>;

int main(){
    int n,s;
    cin >>n >>s;
    --s;

    vector<P> v(n);
    rep(i,n){
        int l,r;
        cin >>l >>r;
        v[i] = {{r,l},i};
    }
    sort(all(v));

    vector<vector<ll>> w(n,vector<ll>(n));
    rep(i,n)rep(j,n) cin >>w[i][j];

    rep(k,n)rep(i,n)rep(j,n) w[i][j] = min(w[i][j], w[i][k]+w[k][j]);

    vector<ll> dp(n);
    rep(i,n){
        int to = v[i].se;
        ll d = w[s][to];
        ll l = v[i].fi.se, r = v[i].fi.fi;

        ll a = max(r-max(l,d), 0LL);
        dp[i] = a;
    }

    rep(i,n){
        int u = v[i].se;
        ll R = v[i].fi.fi;

        for(int j=i+1; j<n; ++j){
            int to = v[j].se;
            ll d = w[u][to];
            ll l = v[j].fi.se, r = v[j].fi.fi;

            ll a = r-max(l,R+d);
            if(a>=0) dp[j] = max(dp[j], dp[i]+a);
        }
    }

    ll ans = 0;
    rep(i,n) ans = max(ans, dp[i]);
    cout << ans << endl;
    return 0;
}
accuracy: 0.7745098039215687
time_ms: 170
memory_kb: 4980
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

using pi = pair<int,int>;
using P = pair<pi,int>;

int main(){
    int n,s;
    cin >>n >>s;
    --s;

    vector<P> v(n);
    rep(i,n){
        int l,r;
        cin >>l >>r;
        v[i] = {{r,l},i};
    }
    sort(all(v));

    vector<vector<ll>> w(n,vector<ll>(n));
    rep(i,n)rep(j,n) cin >>w[i][j];

    rep(k,n)rep(i,n)rep(j,n) w[i][j] = min(w[i][j], w[i][k]+w[k][j]);

    vector<ll> dp(n);
    rep(i,n){
        int to = v[i].se;
        ll d = w[s][to];
        ll l = v[i].fi.se, r = v[i].fi.fi;

        ll a = max(r-max(l,d), 0LL);
        dp[i] = a;
    }

    rep(i,n){
        int u = v[i].se;
        ll R = v[i].fi.fi;

        for(int j=i+1; j<n; ++j){
            int to = v[j].se;
            ll d = w[u][to];
            ll l = v[j].fi.se, r = v[j].fi.fi;

            ll a = max(r-max(l,R+d), 0LL);
            dp[j] = max(dp[j], dp[i]+a);
        }
    }

    ll ans = 0;
    rep(i,n) ans = max(ans, dp[i]);
    cout << ans << endl;
    return 0;
}
accuracy: 0.3137254901960784
time_ms: 120
memory_kb: 4900
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;

int n, s, now = 0, ans = 0;
pair<int, int> lr[505];
int memo[505][505] = {0};
// dp[i][j] =
// i+1番目にjに行ったときの、それまでの布教時間の最大値
int dp[505][505] = {0};

int main() {
  int i, j, k;
  cin >> n >> s;
  --s;
  for(i = 0; i < n; ++i) cin >> lr[i].first >> lr[i].second;
  for(i = 0; i < n; ++i)
    for(j = 0; j < n; ++j) cin >> memo[i][j];
  // wf
  for(k = 0; k < n; ++k)
    for(i = 0; i < n; ++i)
      for(j = 0; j < n; ++j)
        memo[i][j] =
            min(memo[i][j], memo[i][k] + memo[k][j]);
  for(i = 0; i < n; ++i)
    for(j = 0; j < n; ++j) dp[i][j] = -1;
  // i=0はsからの値をそれぞれメモ
  for(i = 0; i < n; ++i)
    dp[0][i] = lr[i].second - max(memo[s][i], lr[i].first);
  // nが1のときはコーナーケースとして処理
  if(n == 1) ans = dp[0][0];
  for(i = 1; i < n; ++i) {
    // jからkに行ったときに増える量を計算し、最大値をメモする
    for(j = 0; j < n; ++j) {
      if(dp[i - 1][j] < 0) continue;
      for(k = 0; k < n; ++k) {
        int x = lr[k].second -
                max(lr[j].second + memo[j][k], lr[k].first);

        //既に計算済みの値と、新しくjからkに行ったときの合計値を比較
        if(x>=0)dp[i][k] =
            max(dp[i][k], dp[i - 1][j] + x);
         ans = max(ans, dp[i][k]);
      }
  	}
  }
  cout  <<  ans  << endl;
  return 0;
}
accuracy: 1
time_ms: 440
memory_kb: 5068
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;

int n, s, now = 0, ans;
pair<int, int> lr[505];
int memo[505][505] = {0};
// dp[i][j] =
// i+1番目にjに行ったときの、それまでの布教時間の最大値
int dp[505][505] = {0};

int main() {
  int i, j, k;
  cin >> n >> s;
  --s;
  for(i = 0; i < n; ++i) cin >> lr[i].first >> lr[i].second;
  for(i = 0; i < n; ++i)
    for(j = 0; j < n; ++j) cin >> memo[i][j];
  // wf
  for(k = 0; k < n; ++k)
    for(i = 0; i < n; ++i)
      for(j = 0; j < n; ++j)
        memo[i][j] =
            min(memo[i][j], memo[i][k] + memo[k][j]);
  // i=0はsからの値をそれぞれメモ
  for(i = 0; i < n; ++i)
    dp[0][i] = lr[i].second - max(memo[s][i], lr[i].first);
  // nが1のときはコーナーケースとして処理
  if(n == 1) ans = dp[0][0];
  for(i = 1; i < n; ++i) {
    // jからkに行ったときに増える量を計算し、最大値をメモする
    for(j = 0; j < n; ++j)
      for(k = 0; k < n; ++k) {
        //既に計算済みの値と、新しくjからkに行ったときの合計値を比較
        dp[i][k] =
            max(dp[i][k], dp[i - 1][j] + lr[k].second -
                              max(lr[j].second + memo[j][k],
                                  lr[k].first));
        if(i == n - 1) ans = max(ans, dp[i][k]);
      }
  }
  cout  <<  ans  << endl;
  return 0;
}
accuracy: 0.7745098039215687
time_ms: 370
memory_kb: 5068
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

int n, s, now = 0, ans;
pair<int, int> lr[505];
int memo[505][505] = {0};
// dp[i][j] =
// i+1番目にjに行ったときの、それまでの布教時間の最大値
int dp[505][505] = {0};

int main() {
  int i, j, k;
  cin >> n >> s;
  --s;
  for(i = 0; i < n; ++i) cin >> lr[i].first >> lr[i].second;
  for(i = 0; i < n; ++i)
    for(j = 0; j < n; ++j) cin >> memo[i][j];
  // wf
  for(k = 0; k < n; ++k)
    for(i = 0; i < n; ++i)
      for(j = 0; j < n; ++j)
        memo[i][j] =
            min(memo[i][j], memo[i][k] + memo[k][j]);
  // i=0はsからの値をそれぞれメモ
  for(i = 0; i < n; ++i)
    dp[0][i] = lr[i].second - max(memo[s][i], lr[i].first);
  for(i = 1; i < n; ++i) {
    // jからkに行ったときに増える量を計算し、最大値をメモする
    for(j = 0; j < n; ++j)
      for(k = 0; k < n; ++k) {
        //既に計算済みの値と、新しくjからkに行ったときの合計値を比較
        dp[i][k] =
            max(dp[i][k], dp[i - 1][j] + lr[k].second -
                              max(lr[j].second + memo[j][k],
                                  lr[k].first));
        if(i == n - 1) ans = max(ans, dp[i][k]);
      }
  }
  cout  <<  ans  << endl;
  return 0;
}
accuracy: 0.09803921568627451
time_ms: 270
memory_kb: 5068
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define cant -5e18
typedef struct 
{
	int l;
	int r;
	int num;
} interval;

int cmp(const void *p, const void *q)
{
	return ((interval*)p)->r-((interval*)q)->r;
}

int main(int argc, char const *argv[])
{
	long long n, s;
	cin >> n >> s;

	interval lr[501]={};
	for (int i = 1; i <= n; ++i)
	{
		cin >> lr[i].l >> lr[i].r;
		lr[i].num=i;
	}

	long long w[n+1][n+1];
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			cin >> w[i][j];
		}
	}

	//ここまで入力

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			for (int k = 1; k <= n; ++k)
			{
				w[j][k] = min(w[j][k], w[j][i]+w[i][k]);
			}
		}
	}

	//ここまでWF

	qsort(lr+1, n, sizeof(interval), cmp);


	for (int i = 1; i <= n; ++i)
	{
		if (s == lr[i].num)
		{
			s = i;
			break;
		}
	}

	long long dp[501]={}, tmp;
	int now = s;
	if (w[lr[s].num][lr[1].num]>lr[1].r) tmp = cant;
	else if (w[lr[s].num][lr[1].num]<lr[1].l) tmp = lr[1].r-lr[1].l;
	else tmp = lr[1].r-w[lr[s].num][lr[1].num];
	dp[1] = tmp;
	for (int i = 2; i <= n; ++i)
	{		
		if (w[lr[s].num][lr[i].num]>lr[i].r) tmp = cant;
		else if (w[lr[s].num][lr[i].num]<lr[i].l) tmp = lr[i].r-lr[i].l;
		else tmp = lr[i].r-w[lr[s].num][lr[i].num];
		dp[i] = tmp;
		for (int j = 1; j < i; ++j)
		{
			if (lr[j].r+w[lr[j].num][lr[i].num]>lr[i].r) tmp = cant;
			else if (lr[j].r+w[lr[j].num][lr[i].num]<lr[i].l) tmp = dp[j]+lr[i].r-lr[i].l;
			else tmp = dp[j]+lr[i].r-w[lr[j].num][lr[i].num]-lr[j].r;
			dp[i] = max(dp[i], tmp);
		}
	}

	long long ans = 0;
	for (int i = 1; i <= n; ++i)
	{	
		ans = max(ans, dp[i]);
	}

	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 5232
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define cant -5e18
typedef struct 
{
	int l;
	int r;
	int num;
} interval;

int cmp(const void *p, const void *q)
{
	return ((interval*)p)->r-((interval*)q)->r;
}

int main(int argc, char const *argv[])
{
	long long n, s;
	cin >> n >> s;

	interval lr[501]={};
	for (int i = 1; i <= n; ++i)
	{
		cin >> lr[i].l >> lr[i].r;
		lr[i].num=i;
	}

	long long w[n+1][n+1];
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			cin >> w[i][j];
		}
	}

	//ここまで入力

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			for (int k = 1; k <= n; ++k)
			{
				w[j][k] = min(w[j][k], w[j][i]+w[i][k]);
			}
		}
	}

	//ここまでWF

	qsort(lr+1, n, sizeof(interval), cmp);


	for (int i = 1; i <= n; ++i)
	{
		if (s == lr[i].num)
		{
			s = i;
			break;
		}
	}

	long long dp[501]={}, tmp;
	int now = s;
	if (w[lr[s].num][lr[1].num]>lr[1].r) tmp = cant;
	else if (w[lr[s].num][lr[1].num]<lr[1].l) tmp = lr[1].r-lr[1].l;
	else tmp = lr[1].r-w[s][lr[1].num];
	dp[1] = tmp;
	for (int i = 2; i <= n; ++i)
	{		
		if (w[lr[s].num][lr[i].num]>lr[i].r) tmp = cant;
		else if (w[lr[s].num][lr[i].num]<lr[i].l) tmp = lr[i].r-lr[i].l;
		else tmp = lr[i].r-w[s][lr[i].num];
		dp[i] = tmp;
		for (int j = 1; j < i; ++j)
		{
			if (lr[j].r+w[lr[j].num][lr[i].num]>lr[i].r) tmp = cant;
			else if (lr[j].r+w[lr[j].num][lr[i].num]<lr[i].l) tmp = dp[j]+lr[i].r-lr[i].l;
			else tmp = dp[j]+lr[i].r-w[lr[j].num][lr[i].num]-lr[j].r;
			dp[i] = max(dp[i], tmp);
		}
	}

	long long ans = 0;
	for (int i = 1; i <= n; ++i)
	{	
		ans = max(ans, dp[i]);
	}

	cout << ans << endl;
	return 0;
}
accuracy: 0.29411764705882354
time_ms: 120
memory_kb: 5180
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define cant -5e18
typedef struct 
{
	int l;
	int r;
	int num;
} interval;

int cmp(const void *p, const void *q)
{
	return ((interval*)p)->r-((interval*)q)->r;
}

int main(int argc, char const *argv[])
{
	long long n, s;
	cin >> n >> s;

	interval lr[501]={};
	for (int i = 1; i <= n; ++i)
	{
		cin >> lr[i].l >> lr[i].r;
		lr[i].num=i;
	}

	long long w[n+1][n+1];
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			cin >> w[i][j];
		}
	}

	//ここまで入力

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			for (int k = 1; k <= n; ++k)
			{
				w[j][k] = min(w[j][k], w[j][i]+w[i][k]);
			}
		}
	}

	//ここまでWF

	qsort(lr+1, n, sizeof(interval), cmp);


	for (int i = 1; i <= n; ++i)
	{
		if (s == lr[i].num)
		{
			s = i;
			break;
		}
	}

	long long dp[501]={}, tmp;
	int now = s;
	if (w[lr[s].num][lr[1].num]>lr[1].r) tmp = cant;
	else if (w[lr[s].num][lr[1].num]<lr[1].l) tmp = lr[1].r-lr[1].l;
	else tmp = lr[1].r-w[s][lr[1].num];
	dp[1] = tmp;
	for (int i = 2; i <= n; ++i)
	{		
		if (w[s][lr[i].num]>lr[i].r) tmp = cant;
		else if (w[s][lr[i].num]<lr[i].l) tmp = lr[i].r-lr[i].l;
		else tmp = lr[i].r-w[s][lr[i].num];
		dp[i] = tmp;
		for (int j = 1; j < i; ++j)
		{
			if (lr[j].r+w[lr[j].num][lr[i].num]>lr[i].r) tmp = cant;
			else if (lr[j].r+w[lr[j].num][lr[i].num]<lr[i].l) tmp = dp[j]+lr[i].r-lr[i].l;
			else tmp = dp[j]+lr[i].r-w[lr[j].num][lr[i].num]-lr[j].r;
			dp[i] = max(dp[i], tmp);
		}
	}

	long long ans = 0;
	for (int i = 1; i <= n; ++i)
	{	
		ans = max(ans, dp[i]);
	}

	cout << ans << endl;
	return 0;
}
accuracy: 0.29411764705882354
time_ms: 110
memory_kb: 5228
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define cant -1e18

struct interval {
  int l, r, num;
};

interval lr[505];
long long w[505][505];
long long dp[505];

int main() {
  long long n, s;
  cin >> n >> s;
  s--;

  for (int i = 0; i < n; i++) {
    cin >> lr[i].l >> lr[i].r;
    lr[i].num = i;
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> w[i][j];
    }
  }

  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for(int j = 0; j < n; j++) {
        w[i][j] = min(w[i][j], w[i][k] + w[k][j]);
      }
    }
  }

  //ここまでWF

  sort(lr, lr+n, [&](const interval &a, const interval &b){
    return a.r < b.r;
  });

  for (int i = 0; i < n; i++) if (s == lr[i].num) {
    s = i;
    break;
  }

  int sn = lr[s].num;

  for (int i = 0; i < n; i++) {
    int in = lr[i].num;
    long long tmp = lr[i].r - max<long long>(lr[i].l, w[sn][in]);
    if(tmp >= 0) dp[i] = tmp;
    else dp[i] = cant;

    for (int j = 0; j < i; j++) {
      int jn = lr[j].num;
      if (lr[j].r + w[jn][in] < lr[i].r) {
        long long nv = dp[j] + lr[i].r - max<long long>(lr[i].l, lr[j].r + w[jn][in]);
        dp[i] = max<long long>(dp[i], nv);
      }
    }
  }

  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, dp[i]);
  }

  cout << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 5136
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define cant -1e18

struct interval {
	int l, r, num;
};

interval lr[505];
long long w[505][505];
long long dp[505];

int main() {
	long long n, s;
	cin >> n >> s;
  s--;

	for (int i = 0; i < n; i++) {
		cin >> lr[i].l >> lr[i].r;
		lr[i].num = i;
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> w[i][j];
		}
	}

  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for(int j = 0; j < n; j++) {
        w[i][j] = min(w[i][j], w[i][k] + w[k][j]);
      }
    }
  }

	//ここまでWF

  sort(lr, lr+n, [&](const interval &a, const interval &b){
    return a.r < b.r;
  });

	for (int i = 0; i < n; i++) if (s == lr[i].num) {
		s = i;
		break;
	}

  int sn = lr[s].num;

	for (int i = 0; i < n; i++) {
    int in = lr[i].num;
    long long tmp = lr[i].r - max<long long>(lr[i].l, w[sn][in]);
		if(tmp >= 0) dp[i] = tmp;
    else dp[i] = cant;

		for (int j = 0; j < i; j++) {
      int jn = lr[j].num;
      if (lr[j].r + w[jn][in] < lr[i].r) {
        long long nv = dp[j] + lr[i].r - max<long long>(lr[i].l, lr[j].r + w[jn][in]);
        dp[i] = max<long long>(dp[i], nv);
      }
		}
	}

	long long ans = 0;
	for (int i = 0; i < n; i++) {
		ans = max(ans, dp[i]);
	}

	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 5136
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define cant -1e18

struct interval {
	int l, r, num;
};

interval lr[505];
long long w[505][505];
long long dp[505];

int main() {
	long long n, s;
	cin >> n >> s;

	for (int i = 0; i < n; i++) {
		cin >> lr[i].l >> lr[i].r;
		lr[i].num = i;
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> w[i][j];
		}
	}

  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for(int j = 0; j < n; j++) {
        w[i][j] = min(w[i][j], w[i][k] + w[k][j]);
      }
    }
  }

	//ここまでWF

  sort(lr, lr+n, [&](const interval &a, const interval &b){
    return a.r < b.r;
  });

	for (int i = 0; i < n; i++) if (s == lr[i].num) {
		s = i;
		break;
	}

  int sn = lr[s].num;

	for (int i = 0; i < n; i++) {
    int in = lr[i].num;
    long long tmp = lr[i].r - max<long long>(lr[i].l, w[sn][in]);
		if(tmp >= 0) dp[i] = tmp;
    else dp[i] = cant;

		for (int j = 0; j < i; j++) {
      int jn = lr[j].num;
      if (lr[j].r + w[jn][in] < lr[i].r) {
        long long nv = dp[j] + lr[i].r - max<long long>(lr[i].l, lr[j].r + w[jn][in]);
        dp[i] = max<long long>(dp[i], nv);
      }
		}
	}

	long long ans = 0;
	for (int i = 0; i < n; i++) {
		ans = max(ans, dp[i]);
	}

	cout << ans << endl;
	return 0;
}
accuracy: 0.2549019607843137
time_ms: 120
memory_kb: 5136
final_rank: 19

code_snippet: #include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n,s;
int l[505],r[505];
int cost[505][505];
int dp[505][501];
vector<int> tid;

int get_time(int a,int b,int l,int r){
	int va=min(b,r)-max(a,l);
	return max(0,va);
}

int main(void){
	scanf("%d%d",&n,&s);
	s--;
	for(int i=0;i<n;i++){
		scanf("%d%d",&l[i],&r[i]);
		tid.push_back(r[i]);
	}
	tid.push_back(0);
	sort(tid.begin(),tid.end());
	tid.erase(unique(tid.begin(),tid.end()),tid.end());

	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			scanf("%d",&cost[i][j]);
		}
	}
	for(int k=0;k<n;k++){
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
			}
		}
	}

	memset(dp,-1,sizeof(dp));
	dp[0][s]=0;
	for(int i=0;i+1<tid.size();i++){
		for(int j=0;j<n;j++){
			if(dp[i][j]<0)continue;
			//printf("%d %d %d\n",i,j,dp[i][j]);
			for(int k=0;k<n;k++){
				int nc=tid[i]+cost[j][k];
				if(nc>=tid.back())continue;
				int v=upper_bound(tid.begin(),tid.end(),nc)-tid.begin();
				//printf("%d->%d %d %d %d %d\n",j,k,nc,tid[v],l[k],r[k]);
				int val=get_time(nc,tid[v],l[k],r[k]);
				dp[v][k]=max(dp[v][k],dp[i][j]+val);
			}
		}
	}
	int ans=0;
	for(int i=0;i<n;i++){
		ans=max(ans,dp[tid.size()-1][i]);
	}
	printf("%d\n",ans);
	return 0;
}
accuracy: 1
time_ms: 2830
memory_kb: 5236
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define cant -5e18
typedef struct 
{
	int l;
	int r;
	int num;
} interval;

int cmp(const void *p, const void *q)
{
	return ((interval*)p)->r-((interval*)q)->r;
}

int main(int argc, char const *argv[])
{
	long long n, s;
	cin >> n >> s;

	interval lr[501]={};
	for (int i = 1; i <= n; ++i)
	{
		cin >> lr[i].l >> lr[i].r;
		lr[i].num=i;
	}

	long long w[n+1][n+1];
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			cin >> w[i][j];
		}
	}

	//ここまで入力

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			for (int k = 1; k <= n; ++k)
			{
				w[j][k] = min(w[j][k], w[j][i]+w[i][k]);
			}
		}
	}

	//ここまでWF

	qsort(lr+1, n, sizeof(interval), cmp);


	for (int i = 1; i <= n; ++i)
	{
		if (s == lr[i].num)
		{
			s = i;
			break;
		}
	}

	long long dp[501]={}, tmp;
	int now = s;
	if (w[s][lr[1].num]>lr[1].r) tmp = cant;
	else if (w[s][lr[1].num]<lr[1].l) tmp = lr[1].r-lr[1].l;
	else tmp = lr[1].r-w[s][lr[1].num];
	dp[1] = tmp;
	for (int i = 2; i <= n; ++i)
	{		
		if (w[s][lr[i].num]>lr[i].r) tmp = cant;
		else if (w[s][lr[i].num]<lr[i].l) tmp = lr[i].r-lr[i].l;
		else tmp = lr[i].r-w[s][lr[i].num];
		dp[i] = tmp;
		for (int j = 1; j < i; ++j)
		{
			if (lr[j].r+w[lr[j].num][lr[i].num]>lr[i].r) tmp = cant;
			else if (lr[j].r+w[lr[j].num][lr[i].num]<lr[i].l) tmp = dp[j]+lr[i].r-lr[i].l;
			else tmp = dp[j]+lr[i].r-w[lr[j].num][lr[i].num]-lr[j].r;
			dp[i] = max(dp[i], tmp);
		}
	}

	long long ans = 0;
	for (int i = 1; i <= n; ++i)
	{	
		ans = max(ans, dp[i]);
	}

	cout << ans << endl;
	return 0;
}
accuracy: 0.29411764705882354
time_ms: 120
memory_kb: 5140
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define cant -5e18
typedef struct 
{
	int l;
	int r;
	int num;
} interval;

int cmp(const void *p, const void *q)
{
	return ((interval*)p)->r-((interval*)q)->r;
}

int main(int argc, char const *argv[])
{
	long long n, s;
	cin >> n >> s;

	interval lr[501]={};
	for (int i = 1; i <= n; ++i)
	{
		cin >> lr[i].l >> lr[i].r;
		lr[i].num=i;
	}

	long long w[n+1][n+1];
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			cin >> w[i][j];
		}
	}

	//ここまで入力

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			for (int k = 1; k <= n; ++k)
			{
				w[j][k] = min(w[j][k], w[j][i]+w[i][k]);
			}
		}
	}

	//ここまでWF

	qsort(lr+1, n, sizeof(interval), cmp);


	for (int i = 1; i <= n; ++i)
	{
		if (s == lr[i].num)
		{
			s = i;
			break;
		}
	}

	long long dp[501]={}, tmp;
	int now = s;
	if (w[lr[s].num][lr[1].num]>lr[1].r) tmp = cant;
	else if (w[s][lr[1].num]<lr[1].l) tmp = lr[1].r-lr[1].l;
	else tmp = lr[1].r-w[s][lr[1].num];
	dp[1] = tmp;
	for (int i = 2; i <= n; ++i)
	{		
		if (w[s][lr[i].num]>lr[i].r) tmp = cant;
		else if (w[s][lr[i].num]<lr[i].l) tmp = lr[i].r-lr[i].l;
		else tmp = lr[i].r-w[s][lr[i].num];
		dp[i] = tmp;
		for (int j = 1; j < i; ++j)
		{
			if (lr[j].r+w[lr[j].num][lr[i].num]>lr[i].r) tmp = cant;
			else if (lr[j].r+w[lr[j].num][lr[i].num]<lr[i].l) tmp = dp[j]+lr[i].r-lr[i].l;
			else tmp = dp[j]+lr[i].r-w[lr[j].num][lr[i].num]-lr[j].r;
			dp[i] = max(dp[i], tmp);
		}
	}

	long long ans = 0;
	for (int i = 1; i <= n; ++i)
	{	
		ans = max(ans, dp[i]);
	}

	cout << ans << endl;
	return 0;
}
accuracy: 0.29411764705882354
time_ms: 130
memory_kb: 5148
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define cant -5e18
typedef struct 
{
	int l;
	int r;
	int num;
} interval;

int cmp(const void *p, const void *q)
{
	return ((interval*)p)->r-((interval*)q)->r;
}

int main(int argc, char const *argv[])
{
	long long n, s;
	cin >> n >> s;

	interval lr[501]={};
	for (int i = 1; i <= n; ++i)
	{
		cin >> lr[i].l >> lr[i].r;
		lr[i].num=i;
	}

	long long w[n+1][n+1];
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			cin >> w[i][j];
		}
	}

	//ここまで入力

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			for (int k = 1; k <= n; ++k)
			{
				w[j][k] = min(w[j][k], w[j][i]+w[i][k]);
			}
		}
	}

	//ここまでWF

	qsort(lr+1, n, sizeof(interval), cmp);


	for (int i = 1; i <= n; ++i)
	{
		if (s == lr[i].num)
		{
			s = i;
			break;
		}
	}

	long long dp[501]={}, tmp;
	int now = s;
	if (w[lr[s].num][lr[1].num]>lr[1].r) tmp = cant;
	else if (w[s][lr[1].num]<lr[1].l) tmp = lr[1].r-lr[1].l;
	else tmp = lr[1].r-w[s][lr[1].num];
	dp[1] = max((long long)0, tmp);
	for (int i = 2; i <= n; ++i)
	{		
		if (w[s][lr[i].num]>lr[i].r) tmp = cant;
		else if (w[s][lr[i].num]<lr[i].l) tmp = lr[i].r-lr[i].l;
		else tmp = lr[i].r-w[s][lr[i].num];
		dp[i] = max((long long)0, tmp);
		for (int j = 1; j < i; ++j)
		{
			if (lr[j].r+w[lr[j].num][lr[i].num]>lr[i].r) tmp = cant;
			else if (lr[j].r+w[lr[j].num][lr[i].num]<lr[i].l) tmp = dp[j]+lr[i].r-lr[i].l;
			else tmp = dp[j]+lr[i].r-w[lr[j].num][lr[i].num]-lr[j].r;
			dp[i] = max(dp[i], tmp);
		}
	}

	long long ans = 0;
	for (int i = 1; i <= n; ++i)
	{	
		ans = max(ans, dp[i]);
	}

	cout << ans << endl;
	return 0;
}
accuracy: 0.29411764705882354
time_ms: 120
memory_kb: 5172
final_rank: 16