Problem:
Problem J: Tree Construction
Consider a two-dimensional space with a set of points (
x
i
,
y
i
) that satisfy
x
i
<
x
j
and
y
i
>
y
j
for all
i < j
.
We want to have them all connected by a directed tree whose edges go toward
either right (
x
positive) or upward (
y
positive). The
figure below shows an example tree.
Figure 1: An example tree
Write a program that finds a tree connecting all given points with the
shortest total length of edges.
Input
The input begins with a line that contains an integer
n
(1 <=
n
<= 1000), the number of points. Then
n
lines follow. The
i
-th line contains two integers
x
i
and
y
i
(0 <=
x
i
,
y
i
<= 10000), which give
the coordinates of the
i
-th point.
Output
Print the total length of edges in a line.
Sample Input 1
5
1 5
2 4
3 3
4 2
5 1
Output for the Sample Input 1
12
Sample Input 2
1
10000 0
Output for the Sample Input 2
0

Submitted solutions:

code_snippet: #include<cstdio>
#include<algorithm>
using namespace std;
int x[1024],y[1024],dp[1024][1024];
int main()
{
    int n,minx=1e9,miny=1e9,ans;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
        scanf("%d%d",&x[i],&y[i]);
    for(int i=1;i<=n;i++)
        for(int j=i;j<=n;j++)
            dp[i][j]=1e9;

    for(int i=1;i<=n;i++)
        dp[i][i]=0;

    for(int i=1;i<=n;i++)
        for(int j=1;j+i<=n;j++)
            for(int k=j;k<j+i;k++)
                dp[j][j+i]=min(dp[j][j+i],dp[j][k]+dp[k+1][j+i]+y[k]-y[i+j]+x[k+1]-x[j]);

    printf("%d\n",dp[1][n]);
    return 0;
}
accuracy: 1
time_ms: 370
memory_kb: 7068
final_rank: 10

code_snippet: #include<bits/stdc++.h>

using namespace std;

struct point
{
    int x, y;
};

int main()
{
    int n;
    cin >> n;

    vector<point> pt(n);

    for (auto &to: pt) {
        cin >> to.x >> to.y;
    }

    int dp[n][n];

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

    for (int len = 2; len <= n; ++ len) {
        for (int l = 0; l + len <= n; ++ l) {

            int r = l + len - 1;

            dp[l][r] = 1000000000;

            for (int c = l; c < r; ++ c) {
                dp[l][r] = min( dp[l][r], dp[l][c] + dp[c + 1][r] + (pt[c + 1].x - pt[l].x) +(pt[c].y - pt[r].y));
            }
        }
    }

    cout << dp[0][n - 1] << '\n';

}
accuracy: 1
time_ms: 200
memory_kb: 7276
final_rank: 3

code_snippet: // QCFium 法
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")


#ifndef HIDDEN_IN_VS // 折りたたみ用

// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS

// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;

// 型名の短縮
using ll = long long; using ull = unsigned long long; // -2^63 ～ 2^63 = 9e18（int は -2^31 ～ 2^31 = 2e9）
using pii = pair<int, int>;	using pll = pair<ll, ll>;	using pil = pair<int, ll>;	using pli = pair<ll, int>;
using vi = vector<int>;		using vvi = vector<vi>;		using vvvi = vector<vvi>;	using vvvvi = vector<vvvi>;
using vl = vector<ll>;		using vvl = vector<vl>;		using vvvl = vector<vvl>;	using vvvvl = vector<vvvl>;
using vb = vector<bool>;	using vvb = vector<vb>;		using vvvb = vector<vvb>;
using vc = vector<char>;	using vvc = vector<vc>;		using vvvc = vector<vvc>;
using vd = vector<double>;	using vvd = vector<vd>;		using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;

// 定数の定義
const double PI = acos(-1);
int DX[4] = { 1, 0, -1, 0 }; // 4 近傍（下，右，上，左）
int DY[4] = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003094073385LL; // (int)INFL = INF, (int)(-INFL) = -INF;

// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;

// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), (x)))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), (x)))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順
#define repe(v, a) for(const auto& v : (a)) // a の全要素（変更不可能）
#define repea(v, a) for(auto& v : (a)) // a の全要素（変更可能）
#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d ビット全探索（昇順）
#define repis(i, set) for(int i = lsb(set), bset##i = set; i < 32; bset##i -= 1 << i, i = lsb(bset##i)) // set の全要素（昇順）
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て（昇順）
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定

// 汎用関数の定義
template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新（更新されたら true を返す）
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新（更新されたら true を返す）
template <class T> inline T getb(T set, int i) { return (set >> i) & T(1); }
template <class T> inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // 非負mod

// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }

#endif // 折りたたみ用


#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;

#ifdef _MSC_VER
#include "localACL.hpp"
#endif

//using mint = modint1000000007;
using mint = modint998244353;
//using mint = static_modint<1000000000>;
//using mint = modint; // mint::set_mod(m);

namespace atcoder {
	inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
	inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>;
#endif


#ifdef _MSC_VER // 手元環境（Visual Studio）
#include "local.hpp"
#else // 提出用（gcc）
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : 32; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : 64; }
template <size_t N> inline int lsb(const bitset<N>& b) { return b._Find_first(); }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(...)
#define dump_list(v)
#define dump_mat(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) { vc MLE(1<<30); EXIT(MLE.back()); } } // RE の代わりに MLE を出す
#endif


int main() {
//	input_from_file("input.txt");
//	output_to_file("output.txt");

	int n;
	cin >> n;

	vi x(n), y(n);
	rep(i, n) cin >> x[i] >> y[i];

	vvi dp(n + 1, vi(n + 1, INF));
	rep(l, n) dp[l][l + 1] = 0;

	repir(l, n - 2, 0) repi(r, l + 2, n) {
		repi(m, l + 1, r - 1) {
			chmin(dp[l][r], dp[l][m] + dp[m][r] + x[m] - x[l] + y[m - 1] - y[r - 1]);
		}
	}

	EXIT(dp[0][n]);
}
accuracy: 1
time_ms: 220
memory_kb: 7316
final_rank: 5

code_snippet: #include <cstddef>
#include <cstdint>
#include <iostream>
#include <limits>
#include <vector>

using usize = std::size_t;
using u64 = std::uint_fast64_t;

template <class T> auto md_vec(const usize n, const T &value) {
  return std::vector<T>(n, value);
}
template <class... Args> auto md_vec(const usize n, Args... args) {
  return std::vector<decltype(md_vec(args...))>(n, md_vec(args...));
}

int main() {
  static constexpr u64 Inf = std::numeric_limits<u64>::max();
  usize n;
  std::cin >> n;

  if (n == 1) {
    std::cout << 0 << std::endl;
    return 0;
  }

  std::vector<u64> x(n), y(n);
  for (usize i = 0; i != n; i += 1) {
    std::cin >> x[i] >> y[i];
  }
  auto dph = md_vec(n, n, Inf), dpv = dph;
  auto ah = md_vec(n, n, usize()), av = ah;

  for (usize i = 0; i != n; i += 1) {
    dph[i][i] = 0;
    dpv[i][i] = 0;
    ah[i][i] = i;
    av[i][i] = i;
  }

  for (usize w = 1; w < n - 1; w += 1) {
    for (usize l = 0, r; (r = l + w) < n; l += 1) {
      // dph
      for (usize m = ah[l][r - 1]; m <= ah[l + 1][r]; m += 1) {
        if (!(l <= m && m + 1 <= r)) {
          continue;
        }
        const u64 t = dpv[l][m] + dph[m + 1][r] + (y[l] - y[r]);
        if (dph[l][r] > t) {
          dph[l][r] = t;
          ah[l][r] = m;
        }
      }
      // dpv
      for (usize m = av[l][r - 1]; m <= av[l + 1][r]; m += 1) {
        if (!(l <= m && m + 1 <= r)) {
          continue;
        }
        const u64 t = dpv[l][m] + dph[m + 1][r] + (x[r] - x[l]);
        if (dpv[l][r] > t) {
          dpv[l][r] = t;
          av[l][r] = m;
        }
      }
    }
  }

  u64 ans = Inf;
  for (usize m = 0; m < n - 1; m += 1) {
    const u64 t =
        dpv[0][m] + dph[m + 1][n - 1] + (y[0] - y[n - 1]) + (x[n - 1] - x[0]);
    if (ans > t) {
      ans = t;
    }
  }
  std::cout << ans << std::endl;
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 34128
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
//BEGIN CUT HERE
template<typename T, typename F>
T KnuthYao(int n,F cost){
  vector< vector<T> > dp(n,vector<T>(n));
  vector< vector<int> > ar(n,vector<int>(n));
  for(int i=0;i<n;i++) dp[i][i]=T(0),ar[i][i]=i;
  for(int w=1;w<n;w++){
    for(int i=0;i+w<n;i++){
      int j=i+w;
      int p=ar[i][j-1],q=ar[i+1][j];
      dp[i][j]=dp[i][p]+dp[p+1][j]+cost(i,p,j);
      ar[i][j]=p;
      for(int k=p;k<=q&&k+1<=j;k++){
        T res=dp[i][k]+dp[k+1][j]+cost(i,k,j);
        if(res<dp[i][j]) dp[i][j]=res,ar[i][j]=k;
      }
    }
  }
  return dp[0][n-1];
}
//END CUT HERE
//INSERT ABOVE HERE

signed AOJ_2488(){
  using ll = long long;
  int n;
  cin>>n;
  vector<ll> xs(n),ys(n);
  for(int i=0;i<n;i++) cin>>xs[i]>>ys[i];
  auto calc=
    [&](int i,int k,int j){
      return xs[k+1]-xs[i]+ys[k]-ys[j];
    };
  cout<<KnuthYao<ll>(n,calc)<<endl;
  return 0;
}
/*
  verified on 2018/09/30
  http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2488
*/

signed KUPC2012_J(){
  using ll = long long;
  int n;
  cin>>n;
  vector<ll> ws(n);
  for(int i=0;i<n;i++) cin>>ws[i];
  vector<ll> sm(n+1);
  for(int i=0;i<n;i++) sm[i+1]=sm[i]+ws[i];
  auto cost=[&](int i,int k,int j){(void) k;return sm[j+1]-sm[i];};
  cout<<KnuthYao<ll>(n,cost)<<endl;
  return 0;
}
/*
  verified on 2018/09/30
  https://atcoder.jp/contests/kupc2012/tasks/kupc2012_10
*/

signed main(){
  AOJ_2488();
  //KUPC2012_J();
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 14632
final_rank: 4

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>;

const int N = 1001;
const int INF = INT_MAX/2-1;

int n;
pi p[N];

pi GET(int l, int r){
    return {p[l].fi, p[r].se};
}

int DIST(pi a, pi b){
    return abs(a.fi-b.fi) + abs(a.se-b.se);
}

int dp[N][N];
int dfs(int l, int r){
    if(dp[l][r]>=0) return dp[l][r];
    if(l == r) return 0;

    pi root = GET(l,r);

    int ret = INF;
    for(int i=l; i<r; ++i){
        ret = min(ret, dfs(l,i)+DIST(root,GET(l,i)) + dfs(i+1,r)+DIST(root,GET(i+1,r)));
    }
    // dbg(root);
    // printf(" %d %d :: %d\n",l,r,ret);
    return dp[l][r] = ret;
}

int main(){
    scanf(" %d", &n);
    rep(i,n){
        int x,y;
        scanf(" %d %d", &x, &y);
        p[i] = {x,y};
    }
    memset(dp,-1,sizeof(dp));
    printf("%d\n", dfs(0,n-1));
    return 0;
}
accuracy: 1
time_ms: 840
memory_kb: 7220
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
//INSERT ABOVE HERE

template<typename T,typename F>
T KnuthYao(int n,T INF,F cost){
  vector<vector<T> > dp(n+1,vector<T>(n+1,INF));
  vector<vector<int> > nx(n,vector<int>(n));
  for(int i=0;i<n;i++) dp[i][i]=0,nx[i][i]=i;
  for(int w=1;w<n;w++){
    for(int i=0;i+w<n;i++){
      int j=i+w;
      for(int r=nx[i][j-1];r<=nx[i+1][j];r++){
	T c=dp[i][r]+dp[r+1][j]+cost(i,r,j);
	if(dp[i][j]>c) dp[i][j]=c,nx[i][j]=r;
      }
    }
  }
  return dp[0][n-1];
}

signed AOJ_2488(){  
  Int n;
  cin>>n;
  vector<Int> x(n),y(n);
  for(Int i=0;i<n;i++) cin>>x[i]>>y[i];
  const Int INF = 1e18;
  auto calc=[&](int i,int r,int j){	      
	      return r+1<n?x[r+1]-x[i]+y[r]-y[j]:INF;
	    };
  cout<<KnuthYao(n,INF,calc)<<endl;
  return 0;
}
/*
  verified on 2018/09/30
  http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2488
*/

signed KUPC2012_J(){
  Int n;
  cin>>n;
  vector<Int> w(n);
  for(Int i=0;i<n;i++) cin>>w[i];
  vector<Int> s(n+1);
  for(Int i=0;i<n;i++) s[i+1]=s[i]+w[i];
  const Int INF =1e18;
  auto cost=[&](int i,int r,int j){return s[j+1]-s[i];};
  cout<<KnuthYao(n,INF,cost)<<endl;
  return 0;
}
/*
  verified on 2018/09/30
  https://beta.atcoder.jp/contests/kupc2012/tasks/kupc2012_10
*/

signed main(){
  AOJ_2488();
  //KUPC2012_J();
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 14772
final_rank: 7

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

template<typename T,typename F>
T KnuthYao(int n,T INF,F cost){
  vector<vector<T> > dp(n+1,vector<T>(n+1,INF));
  vector<vector<int> > nx(n,vector<int>(n));
  for(int i=0;i<n;i++) dp[i][i]=0,nx[i][i]=i;
  for(int w=1;w<n;w++){
    for(int i=0;i+w<n;i++){
      int j=i+w;
      for(int r=nx[i][j-1];r<=nx[i+1][j];r++){
	T c=dp[i][r]+dp[r+1][j]+cost(i,r,j);
	if(dp[i][j]>c) dp[i][j]=c,nx[i][j]=r;
      }
    }
  }
  return dp[0][n-1];
}

signed main(){
  Int n;
  cin>>n;
  vector<Int> x(n),y(n);
  for(Int i=0;i<n;i++) cin>>x[i]>>y[i];
  const Int INF = 1e18;
  auto calc=[&](int i,int r,int j){	      
	      return r+1<n?x[r+1]-x[i]+y[r]-y[j]:INF;
	    };
  cout<<KnuthYao(n,INF,calc)<<endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 14680
final_rank: 6

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

template<typename T1,typename T2> void chmin(T1 &a,T2 b){if(a>b) a=b;};
template<typename T1,typename T2> void chmax(T1 &a,T2 b){if(a<b) a=b;};

Int dp[1010][1010];
signed main(){
  Int n;
  cin>>n;
  vector<Int> x(n),y(n);
  for(Int i=0;i<n;i++) cin>>x[i]>>y[i];
  const Int INF = 1e18;
  
  for(Int i=0;i<=n;i++)
    for(Int j=0;j<=n;j++)
      dp[i][j]=i==j?0:INF;

  auto calc=[&](Int i,Int j,Int k){
    return x[k+1]-x[i]+y[k]-y[j];
  };
  
  for(Int l=2;l<=n;l++){
    for(Int i=0;i+l-1<n;i++){
      Int j=i+l-1;
      for(Int k=i;k<j;k++)
	chmin(dp[i][j],dp[i][k]+calc(i,j,k)+dp[k+1][j]);
    }
  }
  cout<<dp[0][n-1]<<endl;
  return 0;
}
accuracy: 1
time_ms: 370
memory_kb: 10996
final_rank: 14

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

const int INF = 1e15;

int N, x[1005], y[1005];
int dp[1005][1005];

void solve(){
  
  if( N == 1 ){
    cout<<0<<endl;
    return;
  }
  
  for(int i=0;i<=N;i++)
    for(int j=0;j<=N;j++) dp[i][j] = INF;
  
  for(int len=0;len<N;len++){
    
    for(int l=0;l<N-len;l++){
      
      int r = l + len;
      
      if( l == r ){
	
	dp[l][r] = 0;
	
      }else{
	
	for(int m=l;m<r;m++){
	  
	  int cost = x[m+1] - x[l] + y[m] - y[r];
	  
	  dp[l][r] = min( dp[l][r], dp[l][m] + dp[m+1][r] + cost );
	  
	}
	
      }
      
    }
    
  }
  
  cout<<dp[0][N-1]<<endl;
  
}

signed main(){
  
  cin>>N;
  
  for(int i=0;i<N;i++) cin>>x[i]>>y[i];
  
  solve();  
  
  return 0;
}
accuracy: 1
time_ms: 280
memory_kb: 10884
final_rank: 12

code_snippet: #include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;

int n,dp[1008][1008],x[1008],y[1008];

int dfs(int l,int r){
  if(dp[l][r]!=1e9)return dp[l][r];
  int res=1e9;
  for(int i=l;i<r;i++){
    dp[l][r]=min(dp[l][r],dfs(l,i)+dfs(i+1,r)+ (y[i]-y[r])+(x[i+1]-x[l]) );
  }
  return dp[l][r];
}

int main(){
  cin>>n;
  r(i,n) cin>>x[i]>>y[i];
  r(i,n)r(j,n) dp[i][j] = (i==j?0:1e9);
  cout<<dfs(0,n-1)<<endl;
}
accuracy: 1
time_ms: 730
memory_kb: 7116
final_rank: 17

code_snippet: #include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;

int n,dp[1008][1008],x[1008],y[1008];

int dfs(int l,int r){
  if(dp[l][r]!=1e9)return dp[l][r];
  int res=1e9;
  for(int i=l;i<r;i++){
    dp[l][r]=min(dp[l][r],dfs(l,i)+dfs(i+1,r)+ (y[i]-y[r])+(x[i+1]-x[l]) );
  }
  return dp[l][r];
}

int main(){
  cin>>n;
  r(i,n) cin>>x[i]>>y[i];
  r(i,n)r(j,n) dp[i][j] = (i==j?0:1e9);
  cout<<dfs(0,n-1)<<endl;
}
accuracy: 1
time_ms: 730
memory_kb: 7120
final_rank: 18

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

template<typename T1,typename T2> void chmin(T1 &a,T2 b){if(a>b) a=b;};
template<typename T1,typename T2> void chmax(T1 &a,T2 b){if(a<b) a=b;};

Int dp[1010][1010];
signed main(){
  Int n;
  cin>>n;
  vector<Int> x(n),y(n);
  for(Int i=0;i<n;i++) cin>>x[i]>>y[i];
  const Int INF = 1e18;
  
  for(Int i=0;i<=n;i++)
    for(Int j=0;j<=n;j++)
      dp[i][j]=i==j?0:INF;

  auto calc=[&](Int i,Int j,Int k){
    return x[k+1]-x[i]+y[k]-y[j];
  };
  
  for(Int l=2;l<=n;l++){
    for(Int i=0;i+l-1<n;i++){
      Int j=i+l-1;
      for(Int k=i;k<j;k++)
	chmin(dp[i][j],dp[i][k]+calc(i,j,k)+dp[k+1][j]);
    }
  }
  cout<<dp[0][n-1]<<endl;
  return 0;
}
accuracy: 1
time_ms: 370
memory_kb: 10900
final_rank: 13

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

int main()
{
	int n;
	cin >> n;
	vector<int> x(n), y(n);
	for (int i = 0; i < n; i++) {
		cin >> x[i] >> y[i];
	}
	vector<vector<int>> dp(n + 1, vector<int>(n + 1, INT_MAX));
	vector<vector<int>> deg(n + 1, vector<int>(n + 1));
	for (int i = 0; i < n; i++) dp[i][i + 1] = 0, deg[i][i + 1] = i;
	for (int l = 2; l <= n; l++) {
		for (int i = 0; i + l <= n; i++) {
			int j = i + l;
			for (int k = max(i + 1, deg[i][j - 1]); k <= min(j - 1, deg[i + 1][j]); k++) {
				int val = dp[i][k] + dp[k][j] + (x[k] - x[i]) + (y[k - 1] - y[j - 1]);
				if (val < dp[i][j]) {
					dp[i][j] = val;
					deg[i][j] = k;
				}
			}
		}
	}
	cout << dp[0][n] << endl;
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 10672
final_rank: 2

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

int main()
{
	int n;
	cin >> n;
	vector<int> x(n), y(n);
	for (int i = 0; i < n; i++) {
		cin >> x[i] >> y[i];
	}
	vector<vector<int>> dp(n + 1, vector<int>(n + 1, INT_MAX));
	for (int i = 0; i < n; i++) dp[i][i + 1] = 0;
	for (int l = 2; l <= n; l++) {
		for (int i = 0; i + l <= n; i++) {
			int j = i + l;
			for (int k = i + 1; k < j; k++) {
				dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + (x[k] - x[i]) + (y[k - 1] - y[j - 1]));
			}
		}
	}
	cout << dp[0][n] << endl;
	return 0;
}
accuracy: 1
time_ms: 310
memory_kb: 6736
final_rank: 8

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 1000

struct Info{
	int x,y;
};

int dp[NUM][NUM];
Info info[NUM];

int main(){

	int N;
	scanf("%d",&N);

	for(int i = 0; i < N; i++){
		scanf("%d %d",&info[i].x,&info[i].y);
	}

	for(int i = 0; i < N; i++){
		dp[i][i] = 0;
		for(int k = i+1; k < N; k++){
			dp[i][k] = BIG_NUM;
		}
	}

	int tmp_cost;

	for(int a = 0; a < N; a++){
		for(int i = 0; i+a < N; i++){
			for(int k = i; k < i+a; k++){
				tmp_cost = abs(info[i].x-info[k+1].x)+abs(info[k].y-info[i+a].y);
				dp[i][i+a] = min(dp[i][i+a],dp[i][k]+dp[k+1][i+a]+tmp_cost);
			}
		}
	}

	printf("%d\n",dp[0][N-1]);

	return 0;
}
accuracy: 1
time_ms: 380
memory_kb: 7140
final_rank: 11

code_snippet: #include <cstdio>
#include <algorithm>
int n, x[1009], y[1009], dp[1009][1009];
int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++) scanf("%d%d", &x[i], &y[i]);
	for (int i = 2; i <= n; i++) {
		for (int l = 0; l <= n - i; l++) {
			int r = l + i; dp[l][r] = 999999999;
			for (int k = l + 1; k < r; k++) {
				dp[l][r] = std::min(dp[l][r], dp[l][k] + dp[k][r] + x[k] - x[l] + y[k - 1] - y[r - 1]);
			}
		}
	}
	printf("%d\n", dp[0][n]);
	return 0;
}
accuracy: 1
time_ms: 380
memory_kb: 6660
final_rank: 9

code_snippet: #include<iostream>
#include<algorithm>
using namespace std;
int x[1002], y[1002], n, dp[1002][1002];
int dfs(int p, int q) {
	if (p == q)return 0;
	if (dp[p][q] >= 0)return dp[p][q];
	int maxn = 1LL << 30;
	for (int i = p; i < q; i++) {
		int mx = x[p], my = y[q];
		int G1 = (y[i] - my);
		int G2 = (x[i + 1] - mx);
		int G3 = dfs(p, i) + dfs(i + 1, q);
		maxn = min(maxn, G1 + G2 + G3);
	}
	dp[p][q] = maxn;
	return maxn;
}
int main() {
	cin >> n; for (int i = 1; i <= n; i++) cin >> x[i] >> y[i];
	for (int i = 0; i < 1004004; i++)dp[i / 1002][i % 1002] = -1;
	cout << dfs(1, n) << endl;
	return 0;
}
accuracy: 1
time_ms: 660
memory_kb: 7124
final_rank: 16

code_snippet: #include<cstdio>
#include<algorithm>
using namespace std;
int x[1024],y[1024],dp[1024][1024];
int main()
{
    int n,minx=1e9,miny=1e9,ans;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
        scanf("%d%d",&x[i],&y[i]);
    for(int i=1;i<=n;i++)
        for(int j=i;j<=n;j++)
            dp[i][j]=1e9;

    for(int i=1;i<=n;i++)
        dp[i][i]=0;

    for(int i=1;i<=n;i++)
        for(int j=1;j+i<=n;j++)
            for(int k=j;k<j+i;k++)
                dp[j][j+i]=min(dp[j][j+i],dp[j][k]+dp[k+1][j+i]+y[k]-y[i+j]+x[k+1]-x[j]);

    printf("%d\n",dp[1][n]);
    return 0;
}
accuracy: 1
time_ms: 620
memory_kb: 5132
final_rank: 15

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
int dp[1100][1100];
int v[1100][1100];
int x[1100];
int y[1100];
int main(){
	int a;scanf("%d",&a);
	for(int i=0;i<a;i++)scanf("%d%d",x+i,y+i);
	for(int i=0;i<a;i++){
		dp[i][i]=0;v[i][i]=i;
	}
	for(int i=1;i<a;i++){
		for(int j=0;i+j<a;j++){
			int now=999999999;
			for(int k=v[j][j+i-1];k<=v[j+1][j+i];k++){
			//	if(k==a-1)continue;
				if(k<j||k>=j+i)continue;
				int cost=dp[j][k]+dp[k+1][j+i]+y[k]-y[i+j]+x[k+1]-x[j];
			//	printf("%d %d %d %d\n",y[k],y[i+j],x[k+1],x[j]);
				if(now>cost){
					v[j][j+i]=k;
					now=cost;
				}
			}
			dp[j][j+i]=now;
		//	printf("%d %d: %d %d\n",j,j+i,now,v[j][j+i]);
		}
	}
	printf("%d\n",dp[0][a-1]);
}
accuracy: 1
time_ms: 10
memory_kb: 10044
final_rank: 1