#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> usingnamespace std; #define MAXN 1016 int n; int x[MAXN] , y[MAXN]; int dp[MAXN][MAXN] , mn[MAXN][MAXN]; intmain(){ while( cin >> n ) { for( int i = 1 ; i <= n ; ++ i ) scanf("%d%d",&x[i],&y[i]); memset( dp , 0x3f , sizeof dp ); for( int i = 1 ; i <= n ; ++ i ) dp[i][i] = 0 , mn[i][i] = i; for( int len = 2 ; len <= n ; ++ len ) { for( int i = 1 ; i + len - 1 <= n ; ++ i ) { int j = i + len - 1; for( int k = mn[i][j - 1] ; k <= mn[i + 1][j] ; ++ k ) { int t = dp[i][k] + dp[k + 1][j] + x[k+1] - x[i] + y[k] - y[j]; if( t < dp[i][j] ) mn[i][j] = k , dp[i][j] = t; } } } cout << dp[1][n] << endl; } }