Problem:
G Katsusando
問題文
お腹がすいたhenoくんは、カツサンドを食べることにしました。
直線上にカツが $N$ 個あります。 $i$ 個目のカツは位置 $X_i$ にあり、その重さは $W_i$ です。henoくんは、次のようにしてカツサンドを作ります。
パンを $1$ 個ずつ持ったやむなくくんとてんぷらくんを、直線上の好きな位置に配置する。 $2$ 人を同じ位置に配置してもよい。この操作にかかる時間は $1$ 秒である。カツのある位置に配置した場合、彼らはその場にあるカツを拾い、パンの上に載せる。 $2$ 人が同じ位置にいる場合は、やむなくくんがカツを拾う。カツを拾うのにかかる時間は無視できる。
やむなくくんとてんぷらくんが同じ位置にいる場合、 $2$ 人を回収し、彼らの持っているパンと(存在すれば)カツを合わせてカツサンドにし、食べる。この操作にかかる時間は $K$ 秒である。カツがもう直線上に存在しない場合は終了する。そうでない場合、1に戻る。
2でないとき、やむなくくんとてんぷらくんの好きな方を選び、左右好きな方に距離 $1$ だけ動かす。この操作にかかる時間は( $1+$ 移動させる人のパンの上に載っているカツの重さの総和 ) 秒である。移動先にカツがある場合、その場にあるカツを拾い、パンの上に載せる。カツを拾うのにかかる時間は無視できる。
henoくんが適切に操作を行ったとき、すべてのカツを食べきるのにかかる最短の時間は何秒でしょうか。
入力
入力は以下の形式で標準入力から与えられる。
$N$ $K$
$X_1$ $W_1$
$X_2$ $W_2$
$\vdots$
$X_N$ $W_N$
制約
入力はすべて整数である。
$1 \leq N \leq 2000$
$1 \leq K \leq 10^9$
$1 \leq X_1 \lt X_2 \lt \cdots \lt X_N \leq 10^5$
$1 \leq W_i \leq 10^5 \ (1 \leq i \leq N)$
出力
答えを 1 行に出力せよ。
入力例1
3 10
3 8
10 3
12 2
出力例1
28
次のような行動が最適です。
位置 $3$ にやむなくくんとてんぷらくんを配置する。やむなくくんがカツ $1$ を拾う。 $2$ 人は同じ位置にいるので、カツサンドが完成し、henoくんがそれを食べる。これには $1+10=11$ 秒かかる。
位置$10$にやむなくくんを、位置 $12$ にてんぷらくんを配置する。 $2$ 人はそれぞれカツ $2$ 、カツ $3$ を拾う。これには $1$ 秒かかる。
その後、てんぷらくんを $(1+2) \times (12-10) = 6$ 秒かけて位置 $10$ に動かす。これでカツサンドが完成し、henoくんが $10$ 秒かけてそれを食べる。
全体でかかった時間は $28$ 秒となります。
入力例2
1 334
7 7
出力例2
335
入力例3
7 870894047
2163 41212
15706 26852
19284 72177
38949 13577
45702 69074
80014 45456
86152 80205
出力例3
3785073673

Submitted solutions:

code_snippet: // AOJ #3178 Katsu Sando
// 2025.4.7

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1LL << 60;
#define gc() getchar_unlocked()

int Cin() {
	int n = 0; int c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

int main(){
    int n = Cin();
    ll K = Cin();

    vector<ll> X(n+1), W(n+1);
    for(int i = 1; i <= n; i++) X[i] = Cin(), W[i] = Cin();

    vector<ll> A(n+1,0);
    for(int i = 1; i <= n; i++) A[i] = A[i-1] + W[i];

    vector<ll> P(n+1, 0);
    for(int i = 1; i <= n-1; i++){
        ll d = X[i+1] - X[i];
        P[i] = P[i-1] + d * A[i];
    }
    P[n] = P[n-1];

    vector<vector<ll>> cost(n+1, vector<ll>(n+1, 0));
    for(int l = 1; l <= n; l++){
        cost[l][l] = 0;
        int m_opt = l;
        for(int r = l+1; r <= n; r++){
            auto f = [&](int m) -> ll {
                return (X[m] - X[l]) * (1 - A[l-1])
                     + (X[r] - X[m]) * (1 + A[r])
                     + 2 * P[m-1] - P[l-1] - P[r-1];
            };
            while(m_opt < r && f(m_opt+1) < f(m_opt)) m_opt++;
            cost[l][r] = f(m_opt);
        }
    }

    vector<ll> dp(n+1, INF);
    dp[0] = 0;
    for(int i = 1; i <= n; i++){
        for(int j = 0; j < i; j++){
            ll phase = 1 + cost[j+1][i] + K;
            dp[i] = min(dp[i], dp[j] + phase);
        }
    }
    printf("%lld\n", dp[n]);
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 34996
final_rank: 7

code_snippet: #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define rep(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define drep(i, m, n) for (ll i = m - 1; i >= n; i--)
#define Endl endl
#define all(a) a.begin(), a.end()
#define pr(i, j) make_pair(i, j)
#define isin(x, l, r) (l <= x && x < r)
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define srt(ar) sort(ar.begin(), ar.end())
#define rev(ar) reverse(ar.begin(), ar.end())
#define jge(f, s, t) cout << (f ? s : t) << endl
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define printar(ar)             \
    do                          \
    {                           \
        for (auto dbg : ar)     \
        {                       \
            cout << dbg << " "; \
        }                       \
        cout << endl;           \
    } while (0)
const ll inf = 1e18;
const ld pi = 3.14159265358979;
const ld eps = 1e-9;
template <class T, ll n, ll idx = 0>
auto make_vec(const ll (&d)[n], const T &init) noexcept
{
    if constexpr (idx < n)
        return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));
    else
        return init;
}

template <class T, ll n>
auto make_vec(const ll (&d)[n]) noexcept
{
    return make_vec(d, T{});
}
//////////////// 以下を貼る ////////////////
template <class T>
size_t HashCombine(const size_t seed, const T &v)
{
    return seed ^ (std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
}
/* pair用 */
template <class T, class S>
struct std::hash<std::pair<T, S>>
{
    size_t operator()(const std::pair<T, S> &keyval) const noexcept
    {
        return HashCombine(std::hash<T>()(keyval.first), keyval.second);
    }
};
////////////////////////////////////////////
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, K;
    cin >> n >> K;
    vector<ll> x(n);
    vector<ll> w(n);
    rep(i, 0, n)
    {
        cin >> x[i] >> w[i];
    }
    vector<ll> dp(n + 1, inf);
    dp[0] = 0;
    vector<vector<ll>> dp2(n, vector<ll>(n, inf));
    vector<vector<ll>> dp3(n, vector<ll>(n, inf));
    rep(i, 0, n)
    {
        dp3[i][i] = 0;
        ll spd = 1 + w[i];
        ll t = 0;
        rep(j, i + 1, n)
        {
            t += (x[j] - x[j - 1]) * spd;
            dp3[i][j] = t;
            spd += w[j];
        }
        spd = 1 + w[i];
        t = 0;
        drep(j, i, 0)
        {
            t += (x[j + 1] - x[j]) * spd;
            dp3[i][j] = t;
            spd += w[j];
        }
    }
    rep(i, 0, n)
    {
        ll x = i;
        rep(j, i, n)
        {
            while (true)
            {
                if (x == j)
                {
                    break;
                }
                if (dp3[i][x] + dp3[j][x] < dp3[i][x + 1] + dp3[j][x + 1])
                {
                    break;
                }
                x++;
            }
            dp2[i][j] = dp3[i][x] + dp3[j][x];
        }
    }
    rep(i, 0, n)
    {
        rep(j, 0, i + 1)
        {
            chmin(dp[i + 1], dp[j] + 1 + dp2[j][i] + K);
        }
    }
    cout << dp[n] << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 66048
final_rank: 15

code_snippet: // AOJ #3178
// Katsu Sando 2025.2.6

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
 
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
 
    int N; 
    ll K;
    cin >> N >> K;
    vector<int> X(N), W(N);
    for (int i = 0; i < N; i++){
        cin >> X[i] >> W[i];
    }
    // 前計算：P[i] = W[0] + W[1] + ... + W[i]
    vector<ll> P(N, 0);
    P[0] = W[0];
    for (int i = 1; i < N; i++){
        P[i] = P[i-1] + W[i];
    }
 
    vector<vector<ll>> Lcost(N, vector<ll>(N, 0));
    for (int i = 0; i < N; i++){
        Lcost[i][i] = 0;
        for (int j = i+1; j < N; j++){
            ll sumWeights = (i==0 ? P[j-1] : P[j-1] - P[i-1]);
            Lcost[i][j] = Lcost[i][j-1] + (ll)(X[j] - X[j-1]) * (1 + sumWeights);
        }
    }

    vector<vector<ll>> Rcost(N, vector<ll>(N, 0));
    for (int j = 0; j < N; j++){
        Rcost[j][j] = 0;
        for (int m = j-1; m >= 0; m--){
            ll sumWeights = (m==0 ? P[j] : P[j] - P[m]);
            Rcost[j][m] = Rcost[j][m+1] + (ll)(X[m+1]-X[m]) * (1 + sumWeights);
        }
    }
 
    const ll INF = 1LL << 60;
    vector<ll> dp(N, INF);

    auto bestForSegment = [&](int i, int j) -> ll {
        int lo = i, hi = j;
        while(hi - lo >= 3){
            int m1 = lo + (hi - lo) / 3;
            int m2 = hi - (hi - lo) / 3;
            ll f1 = Lcost[i][m1] + Rcost[j][m1];
            ll f2 = Lcost[i][m2] + Rcost[j][m2];
            if(f1 > f2)
                lo = m1;
            else
                hi = m2;
        }
        ll best = INF;
        for (int m = lo; m <= hi; m++){
            best = min(best, Lcost[i][m] + Rcost[j][m]);
        }
        return best;
    };

    for (int j = 0; j < N; j++){
        for (int i = 0; i <= j; i++){
            ll segTime = 1 + K + bestForSegment(i, j);
            ll prev = (i-1 >= 0 ? dp[i-1] : 0);
            dp[j] = min(dp[j], prev + segTime);
        }
    }
    cout << dp[N-1] << endl;
    return 0;
}
accuracy: 0.9166666666666666
time_ms: 220
memory_kb: 65908
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001



#define SIZE 2005

ll N,K;
ll X[SIZE],W[SIZE],dp[SIZE];
ll SUM[SIZE],rui_W[SIZE];
ll COST[SIZE][SIZE];

ll to_R(int left,int right){

	return SUM[right]-SUM[left]-rui_W[left]*(X[right]-X[left]);
}

ll to_L(int left,int right){

	return (rui_W[right+1]-rui_W[left])*(X[right]-X[left])-to_R(left,right);
}

ll func(int left,int right,int k){

	return to_R(left,k) + to_L(k,right);
}

int main(){

	scanf("%lld %lld",&N,&K);

	for(ll i = 0; i < N; i++){

		scanf("%lld %lld",&X[i],&W[i]);
	}

	for(int i = 0; i < N; i++){

		rui_W[i+1] = rui_W[i]+W[i];
		SUM[i+1] = SUM[i] + rui_W[i+1]*(X[i+1]-X[i]);
	}

	for(int left = 0; left < N; left++){
		int k = left;
		for(int right = left; right < N; right++){
			while(k < right && func(left,right,k) > func(left,right,k+1)){

				k++;
			}
			COST[left][right] = func(left,right,k) + (X[right]-X[left]) + K+1;
		}
	}

	for(int i = 0; i <= N; i++){

		dp[i] = HUGE_NUM;
	}

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

			dp[i] = min(dp[i],dp[j]+COST[j][i-1]);
		}
	}

	printf("%lld\n",dp[N]);

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 25904
final_rank: 6

code_snippet: //#define _GLIBCXX_DEBUG

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

#define endl '\n'
#define lfs cout<<fixed<<setprecision(10)
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define spa << " " <<
#define fi first
#define se second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)
using ll = long long;
using ld = long double;
const ll MOD1 = 1e9+7;
const ll MOD9 = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
template<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}  
template<typename T>void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout spa v[i][j];cout<<endl;}};
void debug(vector<string>&v,ll h,ll w){for(ll i=0;i<h;i++){for(ll j=0;j<w;j++)cout<<v[i][j];cout<<endl;}};
template<typename T>void debug(vector<T>&v,ll n){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout spa v[i];cout<<endl;};
template<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
vector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1};
template<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}
template<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << p.first << " " << p.second;}
template<typename T>ostream &operator<<(ostream &os, const vector<T> &v){for(auto &z:v)os << z << " ";cout<<"|"; return os;}
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());

int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  ll res=0,buf=0;
  bool judge = true;
  ll n,k;cin>>n>>k;
  vector<ll>x(n),w(n);
  rep(i,0,n)cin>>x[i]>>w[i];
  auto dp=vec(n+1,n+1,INF);
  dp[0][0]=0;
  vector<ll>y(n+1);
  rep(i,0,n)y[i+1]=y[i]+x[i];
  rep(i,0,n){
    ll mi=INF;
    rep(j,0,n+1){
      if(j<n)chmin(mi,dp[i][j]-x[j]);
      chmin(dp[i+1][j],dp[i][j]);
      if(j>0)chmin(dp[i+1][j],mi+k+x[j-1]+1);
      if(j<n)mi+=abs(x[j]-x[i])*w[j];
    }
  }
  //debug(dp,n+1,n+1);
  cout<<dp[n][n]<<endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 34320
final_rank: 4

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

using Int = long long;
const char newl = '\n';

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=Int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}

//INSERT ABOVE HERE
const Int MAX = 2020;
Int dpL[MAX][MAX]={};
Int dpR[MAX][MAX]={};
Int cst[MAX][MAX]={};

Int dp[MAX]={};
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  Int n,k;
  cin>>n>>k;
  vector<Int> xs(n),ws(n);
  for(Int i=0;i<n;i++) cin>>xs[i]>>ws[i];

  // dpL: [i, j]
  for(Int i=0;i<n;i++){
    Int res=0,wei=1;
    dpL[i][i]=res;
    wei+=ws[i];
    for(Int j=i+1;j<n;j++){
      res+=wei*abs(xs[j]-xs[j-1]);
      dpL[i][j]=res;
      wei+=ws[j];
    }
  }

  // dpR: [i, j]
  for(Int j=n-1;j>=0;j--){
    Int res=0,wei=1;
    dpL[j][j]=res;
    wei+=ws[j];
    for(Int i=j-1;i>=0;i--){
      res+=wei*abs(xs[i]-xs[i+1]);
      dpR[i][j]=res;
      wei+=ws[i];
    }
  }

  for(Int i=0;i<n;i++){
    for(Int j=i;j<n;j++){
      if(i==j){
        cst[i][j]=0;
        continue;
      }
      auto calc=[&](Int m){return dpL[i][m]+dpR[m][j];};
      Int l=i,r=j;
      while(l+1<r){
        Int m=(l+r)>>1;
        if(calc(m)<calc(m+1)) r=m;
        else l=m;
      }
      cst[i][j]=min(calc(l),calc(r));
    }
  }

  const Int INF = 1e18;
  fill(dp,dp+MAX,INF);
  dp[0]=0;
  for(Int i=0;i<n;i++)
    for(Int j=i;j<n;j++)
      chmin(dp[j+1],dp[i]+(1+cst[i][j]+k));
  cout<<dp[n]<<endl;
  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 97988
final_rank: 19

code_snippet: #include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL<<60);

template<class T> bool chmin(T &a, const T &b){
    if(a > b) {a = b; return true;}
    else return false;
}

void fill_memo(vec &x, vec &w, mat &memo){
    vec ws(N + 1), muls(N), muls_rev(N);
    ws[0] = muls[0] = muls_rev[N-1] = 0;
    rep(i, N) {
        ws[i+1] = ws[i] + w[i];
        if(i != N - 1) muls[i+1] = muls[i] + ws[i + 1] * (x[i+1] - x[i]);
    }
    Rrep(i, N - 1) muls_rev[i] = muls_rev[i+1] + (ws[N] - ws[i + 1]) * (x[i+1] - x[i]);
    rep(l, N){
        reps(r, l + 1, N + 1){
            int id = lower_bound(ALL(ws), (ws[l] + ws[r] + 1) / 2) - (ws.begin() + 1);
            //[l,id] : left, [id, r) : right
            memo[l][r] = K + 1 + (x[r-1] - x[l]) + (muls[id] - muls[l] - ws[l] * (x[id] - x[l])) + (muls_rev[id] - muls_rev[r-1] - (ws[N] - ws[r]) * (x[r-1] - x[id]));
        }
    }
}

int main() {
    cin>>N>>K;
    mat memo(N, vec(N + 1));
    vec x(N), w(N);
    rep(i, N) cin>>x[i]>>w[i];
    fill_memo(x, w, memo);
    vec dp(N + 1, INF);
    dp[0] = 0;
    reps(i, 1, N + 1){
        rep(j, i) chmin(dp[i], dp[j] + memo[j][i]);
    }
    cout<<dp[N]<<endl;
}
accuracy: 1
time_ms: 70
memory_kb: 34452
final_rank: 13

code_snippet: #include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL<<60);

template<class T> bool chmin(T &a, const T &b){
    if(a > b) {a = b; return true;}
    else return false;
}
template<class T> bool chmax(T &a, const T &b){
    if(a < b) {a = b; return true;}
    else return false;
}

void fill_memo(vec &x, vec &w, mat &memo){
    vec ws(N), muls(N), muls_rev(N);
    ws[0] = w[0];
    muls[0] = muls_rev[N-1] = 0;
    rep(i, N - 1) {
        ws[i+1] = ws[i] + w[i + 1];
        muls[i+1] = muls[i] + ws[i] * (x[i+1] - x[i]);
    }
    Rrep(i, N - 1) muls_rev[i] = muls_rev[i+1] + (ws[N-1] - ws[i]) * (x[i+1] - x[i]);
    rep(l, N){
        reps(r, l + 1, N + 1){
            int id = lower_bound(ALL(ws), ((l == 0 ? 0LL : ws[l-1]) + ws[r-1] + 1) / 2) - ws.begin();
            //[l,id] : left, [id, r) : right
            memo[l][r] = K + 1 + (x[r-1] - x[l]) + (muls[id] - muls[l] - (l==0 ? 0 : ws[l-1]) * (x[id] - x[l])) + (muls_rev[id] - muls_rev[r-1] - (ws[N-1] - ws[r-1]) * (x[r-1] - x[id]));
            //cout<<l<<' '<<r<<' '<<id<<' '<<memo[l][r]<<endl;
        }
    }
}

int main() {
    cin>>N>>K;
    mat memo(N, vec(N + 1));
    vec x(N), w(N);
    rep(i, N) cin>>x[i]>>w[i];
    fill_memo(x, w, memo);
    vec dp(N + 1, INF);
    dp[0] = 0;
    reps(i, 1, N + 1){
        rep(j, i) chmin(dp[i], dp[j] + memo[j][i]);
    }
    cout<<dp[N]<<endl;
}
accuracy: 1
time_ms: 60
memory_kb: 34452
final_rank: 11

code_snippet: #pragma region Macros
#pragma GCC optimize("O3")
#pragma GCC target("avx2,avx")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define rep2(i, a, b) for(ll i = a; i <= b; ++i)
#define rep(i, n) for(ll i = 0; i < n; ++i)
#define rep3(i, a, b) for(ll i = a; i >= b; --i)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vll vector<ll>
#define vpi vector<pii>
#define vpll vector<pll>
#define overload2(_1, _2, name, ...) name
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define VEC(type, name, size)                                                                                                                                  \
    vector<type> name(size);                                                                                                                                   \
    IN(name)
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, w)                                                                                                                                   \
    vector<vector<type>> name(h, vector<type>(w));                                                                                                             \
    IN(name)
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)                                                                                                                         \
    vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
#define fi first
#define se second
#define all(c) begin(c), end(c)
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
using namespace std;
string YES[2] = {"NO", "YES"};
string Yes[2] = {"No", "Yes"};
string yes[2] = {"no", "yes"};
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
#define si(c) (int)(c).size()
#define INT(...)                                                                                                                                               \
    int __VA_ARGS__;                                                                                                                                           \
    IN(__VA_ARGS__)
#define LL(...)                                                                                                                                                \
    ll __VA_ARGS__;                                                                                                                                            \
    IN(__VA_ARGS__)
#define STR(...)                                                                                                                                               \
    string __VA_ARGS__;                                                                                                                                        \
    IN(__VA_ARGS)
#define CHR(...)                                                                                                                                               \
    char __VA_ARGS__;                                                                                                                                          \
    IN(__VA_ARGS)
#define DBL(...)                                                                                                                                               \
    double __VA_ARGS__;                                                                                                                                        \
    IN(__VA_ARGS)
int scan() { return getchar(); }
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }
template <class T> void scan(vector<T> &);
template <class T> void scan(vector<T> &a) {
    for(auto &i : a) scan(i);
}
template <class T> void scan(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &... tail) {
    scan(head);
    IN(tail...);
}
template <class T, class S> inline bool chmax(T &a, S b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T, class S> inline bool chmin(T &a, S b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
vi iota(int n) {
    vi a(n);
    iota(all(a), 0);
    return a;
}
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
vector<pll> factor(ll x) {
    vector<pll> ans;
    for(ll i = 2; i * i <= x; i++)
        if(x % i == 0) {
            ans.push_back({i, 1});
            while((x /= i) % i == 0) ans.back().second++;
        }
    if(x != 1) ans.push_back({x, 1});
    return ans;
}
template <class T> vector<T> divisor(T x) {
    vector<T> ans;
    for(T i = 1; i * i <= x; i++)
        if(x % i == 0) {
            ans.pb(i);
            if(i * i != x) ans.pb(x / i);
        }
    return ans;
}
template <typename T> void zip(vector<T> &x) {
    vector<T> y = x;
    sort(all(y));
    for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); }
}
int popcount(ll x) { return __builtin_popcountll(x); }
struct Setup_io {
    Setup_io() {
        ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        cout << fixed << setprecision(15);
    }
} setup_io;
int in() {
    int x;
    cin >> x;
    return x;
}
ll lin() {
    unsigned long long x;
    cin >> x;
    return x;
}

template <typename T> struct edge {
    int from, to;
    T cost;
    int id;
    edge(int to, T cost) : from(-1), to(to), cost(cost) {}
    edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
    edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id) {}
    edge &operator=(const int &x) {
        to = x;
        return *this;
    }
    operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;

using Tree = vector<vector<int>>;
using Graph = vector<vector<int>>;
template <class T> using Wgraph = vector<vector<edge<T>>>;
Graph getG(int n, int m = -1, bool directed = false, int margin = 1) {
    Tree res(n);
    if(m == -1) m = n - 1;
    while(m--) {
        int a, b;
        cin >> a >> b;
        a -= margin, b -= margin;
        res[a].emplace_back(b);
        if(!directed) res[b].emplace_back(a);
    }
    return move(res);
}
template <class T> Wgraph<T> getWg(int n, int m = -1, bool directed = false, int margin = 1) {
    Wgraph<T> res(n);
    if(m == -1) m = n - 1;
    while(m--) {
        int a, b;
        T c;
        cin >> a >> b >> c;
        a -= margin, b -= margin;
        res[a].emplace_back(b, c);
        if(!directed) res[b].emplace_back(a, c);
    }
    return move(res);
}

#define i128 __int128_t
#define ull unsigned long long int
#define TEST                                                                                                                                                   \
    INT(testcases);                                                                                                                                            \
    while(testcases--)
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    for(auto &e : v) cout << e << " ";
    cout << endl;
    return os;
}
template <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &p) {
    cout << "(" << p.fi << ", " << p.se << ")";
    return os;
}
template <class S, class T> string to_string(pair<S, T> p) { return "(" + to_string(p.first) + "," + to_string(p.second) + ")"; }
template <class A> string to_string(A v) {
    if(v.empty()) return "{}";
    string ret = "{";
    for(auto &x : v) ret += to_string(x) + ",";
    ret.back() = '}';
    return ret;
}

void dump() { cerr << endl; }
template <class Head, class... Tail> void dump(Head head, Tail... tail) {
    cerr << to_string(head) << " ";
    dump(tail...);
}
#define endl '\n'
#ifdef _LOCAL
#undef endl
#define debug(x)                                                                                                                                               \
    cout << #x << ": ";                                                                                                                                        \
    dump(x)
#else
#define debug(x)
#endif
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// int rnd(int n) { return uniform_int_distribution<int>(0, n - 1)(rng); }
// ll rndll(ll n) { return uniform_int_distribution<ll>(0, n - 1)(rng); }

template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
#pragma endregion

int main() {
    INT(n);
    LL(k);
    vll x(n), w(n);
    rep(i, n) cin >> x[i] >> w[i];
    vll dp(n + 1, LLONG_MAX);
    dp[0] = 0;
    vll rui(n + 1), W(n + 1);
    rep(i, n) rui[i + 1] = rui[i] + x[i] * w[i];
    rep(i, n) W[i + 1] = w[i] + W[i];
    auto sum = [&](int l, int r) { return rui[r] - rui[l]; };
    auto wsum = [&](int l, int r) { return W[r] - W[l]; };
    rep(i, n) {
        int t = i;
        rep2(j, i + 1, n) {
            while(t < n and wsum(i, t + 1) < wsum(t + 1, j)) t++;
            ll cost = LLONG_MAX;
            chmin(cost, (x[j - 1] - x[i]) + (wsum(i, t + 1) * x[t] - sum(i, t + 1)) + (sum(t + 1, j) - wsum(t + 1, j) * x[t]) + k);
            chmin(dp[j], dp[i] + cost + 1);
        }
    }
    cout << dp[n] << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3540
final_rank: 2

code_snippet: #include <iostream>
#include <vector>

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }

using lint = long long;
constexpr lint INF = 1LL << 60;

void solve() {
    int n, k;
    std::cin >> n >> k;

    std::vector<lint> xs(n + 1), ws(n + 1);
    for (int i = 1; i <= n; ++i) std::cin >> xs[i] >> ws[i];

    // [i, j]を左から右へ移動するときのコスト
    auto lcost = vec(n + 1, vec(n + 1, 0LL));
    for (int l = 1; l <= n; ++l) {
        lint sum = ws[l] + 1;
        for (int r = l + 1; r <= n; ++r) {
            lcost[l][r] = lcost[l][r - 1] + sum * (xs[r] - xs[r - 1]);
            sum += ws[r];
        }
    }

    // [i, j]を右から左へ移動するときのコスト
    auto rcost = vec(n + 1, vec(n + 1, 0LL));
    for (int r = 1; r <= n; ++r) {
        lint sum = ws[r] + 1;
        for (int l = r - 1; l >= 1; --l) {
            rcost[l][r] = rcost[l + 1][r] + sum * (xs[l + 1] - xs[l]);
            sum += ws[l];
        }
    }

    std::vector<lint> bdp(n + 1, INF), rdp(n + 1, INF);
    // 文章中で定義したDPテーブル2つ
    bdp[0] = 0;

    for (int i = 1; i <= n; ++i) {
        for (int l = 0; l < i; ++l) {
            rdp[i] = std::min(rdp[i], bdp[l] + 1 + lcost[l + 1][i]);
        }

        for (int l = 1; l <= i; ++l) {
            bdp[i] = std::min(bdp[i], rdp[l] + rcost[l][i] + k);
        }
    }

    std::cout << bdp[n] << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 66016
final_rank: 16

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }

using lint = long long;
constexpr lint INF = 1LL << 60;

void solve() {
    int n, k;
    std::cin >> n >> k;

    std::vector<lint> xs(n + 1), ws(n + 1);
    for (int i = 1; i <= n; ++i) std::cin >> xs[i] >> ws[i];

    auto lcost = vec(n + 1, vec(n + 1, 0LL));
    for (int l = 1; l <= n; ++l) {
        lint sum = ws[l] + 1;
        for (int r = l + 1; r <= n; ++r) {
            lcost[l][r] = lcost[l][r - 1] + sum * (xs[r] - xs[r - 1]);
            sum += ws[r];
        }
    }

    auto rcost = vec(n + 1, vec(n + 1, 0LL));
    for (int r = 1; r <= n; ++r) {
        lint sum = ws[r] + 1;
        for (int l = r - 1; l >= 1; --l) {
            rcost[l][r] = rcost[l + 1][r] + sum * (xs[l + 1] - xs[l]);
            sum += ws[l];
        }
    }

    std::vector<lint> dp0(n + 1, INF), dp1(n + 1, INF);
    dp0[0] = 0;

    for (int i = 1; i <= n; ++i) {
        for (int l = 0; l < i; ++l) {
            dp1[i] = std::min(dp1[i], dp0[l] + 1 + lcost[l + 1][i]);
        }

        for (int l = 1; l <= i; ++l) {
            dp0[i] = std::min(dp0[i], dp1[l] + rcost[l][i] + k);
        }
    }

    std::cout << dp0[n] << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 66016
final_rank: 16

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

using namespace std;
using ll = long long;
template<class T> using vec = vector<T>;
template<class T> using vvec = vec<vec<T>>;

const ll inf = 1e18;

int main() {
    int N,K;
    cin >> N >> K;
    vec<ll> X(N),W(N),WS(N+1),S(N+1);
    for(int i=0;i<N;i++){
        cin >> X[i] >> W[i];
        S[i+1] = S[i]+W[i]*X[i];
        WS[i+1] = WS[i]+W[i];
    }
    vvec<ll> cost(N+1,vec<ll>(N+1));
    
    auto val = [&](int l,int r,int id){
        assert(l<=id && id<r);
        ll res = (WS[id+1]-WS[l]+1)*X[id]-(S[id+1]-S[l]+X[l]);
        res += S[r]-S[id]+X[r-1]-(WS[r]-WS[id]+1)*X[id];
        return res;
    };

    for(int l=0;l<N;l++){
        int k = l;
        for(int r=l+1;r<=N;r++){
            while(k+1<r && val(l,r,k)>=val(l,r,k+1)) k++;
            cost[l][r] = val(l,r,k);
        }
    }

    vec<ll> dp(N+1,inf);
    dp[0] = 0;
    for(int i=0;i<N;i++){
        for(int j=0;j<=i;j++) dp[i+1] = min(dp[i+1],dp[j]+cost[j][i+1]+K+1);
    }
    cout << dp[N] << "\n";
}
accuracy: 1
time_ms: 30
memory_kb: 34856
final_rank: 10

code_snippet: #include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <ctime>
#include <complex>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 1020000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << "debug: " << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << "debug: " << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//const int mod = 1e9 + 7;
const int mod = 998244353;

ll cost[2020][2020];

int main(){
	ll n,k; cin >> n >> k;
	vl x(n), w(n);
	rep(i,n) cin >> x[i] >> w[i];
	for(int i=0; i<n; i++){
		ll S = w[i] + 1;
		for(int j=i+1; j<n; j++){
			cost[i][j] = cost[i][j-1] + S * (x[j] - x[j-1]);
			S += w[j];
		}
		S = w[i] + 1;
		for(int j=i-1; j>=0; j--){
			cost[i][j] = cost[i][j+1] + S * (x[j+1] - x[j]);
			S += w[j];
		}
	}
	vl dp(n+1,linf);
	dp[0] = 0;
	rep(i,n){
		ll mn = dp[i];
		int m = i;
		for(int j=i-1; j>=0; j--){
			while(m > j && cost[j][m] + cost[i][m] > cost[j][m-1] + cost[i][m-1]) m--;
			chmin(mn, dp[j] + cost[j][m] + cost[i][m]);
		}
		chmin(dp[i+1], mn + k + 1);
	}
	cout << dp[n] << "\n";
}
accuracy: 1
time_ms: 30
memory_kb: 34732
final_rank: 8

code_snippet: #include <bits/stdc++.h>
#define inf (long long)(1e17)
using namespace std;

long long n, k;
vector<long long> x, w, dp;
vector<vector<long long>> cost;

long long solve();

int main() {
  cin >> n >> k;
  x.resize(n);
  w.resize(n);
  for (int i = 0; i < n; ++i) cin >> x[i] >> w[i];
  cout << solve() << endl;
  return 0;
}

long long solve() {
  {  // calc cost from i to j
    cost.assign(n, vector<long long>(n, inf));
    for (int i = 0; i < n; ++i) {
      long long now = 0;
      // i >= j(left)
      for (int j = i; j < n; ++j) {
        now += w[j] * (x[j] - x[i]);
        cost[j][i] = now + x[j] - x[i];
      }
      // i <= j(right)
      now = 0;
      for (int j = i; j >= 0; --j) {
        now += w[j] * (x[i] - x[j]);
        cost[j][i] = now + x[i] - x[j];
      }
    }
  }
  dp.assign(n + 1, inf);
  dp[0] = 0;
  for (int i = 0; i < n; ++i) {
    long long now = inf;
    for (int j = 0; j <= i; ++j) now = min(now, dp[j] + cost[j][i]);
    for (int j = i; j < n; ++j)
      dp[j + 1] = min(dp[j + 1], now + cost[j][i] + k + 1);
  }
  return dp[n];
}
accuracy: 1
time_ms: 30
memory_kb: 34804
final_rank: 9

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
#include <cassert>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template<class T> void chmin(T &a, const T &b){ a = (a < b ? a : b); }
template<class T> void chmax(T &a, const T &b){ a = (a > b ? a : b); }

int main() {
    int n, k;
    cin >> n >> k;
    vector<int> X(n), W(n);
    for (int i = 0; i < n; ++i) {
        scanf("%d %d", &X[i], &W[i]);
    }
    vector<ll> dp(n+1, INF<ll>);
    dp[0] = 0;
    vector<ll> sumW(n+1);
    for (int i = 0; i < n; ++i) sumW[i+1] = sumW[i]+ W[i];
    for (int i = 0; i < n; ++i) {
        int cur = i; ll val = k+1, d = 2*W[i];
        for (int j = i; j < n; ++j) {
            d -= W[j];
            val += (ll)(X[j] - X[cur])*W[j];
            if(j != i) val += (X[j]-X[j-1]);
            while(d < 0) {
                val += d*(X[cur+1]-X[cur]);
                d += W[++cur]*2;
            }
            chmin(dp[j+1], dp[i]+val);
        }
    }
    cout << dp.back() << "\n";
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3276
final_rank: 1

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

ll dp[2020][2020][3];

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n; cin >> n;
	ll K; cin >> K;
	vector<ll> x(n),w(n);
	vector<ll> s(n+1,0);
	for(int i=0;i<n;i++){
		cin >> x[i] >> w[i];
		s[i+1]=s[i]+w[i];
	}
	for(int i=0;i<2020;i++){
		for(int j=0;j<2020;j++){
			for(int k=0;k<3;k++){
				dp[i][j][k]=1e18;
			}
		}
	}
	dp[0][0][0]=1;
	dp[0][0][1]=1;
	dp[0][0][2]=K+1;
	for(int i=1;i<n;i++){
		for(int j=0;j<i;j++){
			for(int k=0;k<3;k++){
				if(k==0){
					dp[i][j][0]=min(dp[i][j][0],dp[i-1][j][0]+(x[i]-x[i-1])*(s[i]-s[j]));
					dp[i][i][1]=min(dp[i][i][1],dp[i-1][j][0]+(x[i]-x[i-1])*(s[i]-s[j])+(x[i]-x[j]));
					dp[i][i][2]=min(dp[i][i][2],dp[i-1][j][0]+(x[i]-x[i-1])*(s[i]-s[j])+K+(x[i]-x[j]));
				}
				else if(k==1){
					dp[i][j][1]=min(dp[i][j][1],dp[i-1][j][1]+(w[i])*(x[i]-x[j]));
					dp[i][i][2]=min(dp[i][i][2],dp[i-1][j][1]+(w[i])*(x[i]-x[j])+K+(x[i]-x[j]));
				}
				else{
					if(i-1==j){
						dp[i][i][0]=min(dp[i][i][0],dp[i-1][j][k]+1);
						dp[i][i][1]=min(dp[i][i][1],dp[i-1][j][k]+1);
						dp[i][i][2]=min(dp[i][i][2],dp[i-1][j][k]+K+1);
					}
				}
			}
		}
	}
	cout << dp[n-1][n-1][2] << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 98912
final_rank: 18

code_snippet: #include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
T div_floor(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T>
T div_ceil(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a > 0 ? (a - 1) / b + 1 : a / b;
}

constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 100010;

int n;
lint K;
int x[2020];
lint w[2020];
lint cost[2020][2020], dp[2020];

int main() {
    scanf("%d%lld", &n, &K);
    rep(i, n) scanf("%d%lld", &x[i], &w[i]);

    rep(l, n) {
        lint lsum, rsum, tmp_cost;
        lsum = rsum = tmp_cost = 0;
        int pos = l;
        For(r, l, n) {
            if (pos == r)
                lsum += w[r];
            else
                rsum += w[r];
            tmp_cost += w[r] * (x[r] - x[pos]);
            while (pos + 1 <= r && (lsum - rsum) * (x[pos + 1] - x[pos]) < 0) {
                tmp_cost += (lsum - rsum) * (x[pos + 1] - x[pos]);
                rsum -= w[pos + 1];
                lsum += w[pos + 1];
                ++pos;
            }
            cost[l][r] = tmp_cost + x[r] - x[l];
        }
    }

    rep(i, n) {
        dp[i] = cost[0][i] + 1 + K;
        rep(j, i) chmin(dp[i], dp[j] + cost[j + 1][i] + 1 + K);
    }
    printf("%lld\n", dp[n - 1]);
}
accuracy: 1
time_ms: 10
memory_kb: 34528
final_rank: 5

code_snippet: /**
 *    author:  otera    
**/
#include<iostream>
#include<string> 
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm> 
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
using namespace std;

#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

void solve() {
	int n, k; cin >> n >> k;
    vector<int> x(n), w(n);
    rep(i, n) {
        cin >> x[i] >> w[i];
    }
    vector<int> left(n, 0), right(n + 1, 0);
    int sum = 0;
    vector<int> lsum(n + 1, 0), rsum(n + 1, 0);
    rep(i, n) {
        lsum[i + 1] = lsum[i] + w[i];
    }
    for(int i = n - 1; i >= 0; -- i) {
        rsum[i] = rsum[i + 1] + w[i];
    }
    for(int i = 1; i < n; ++ i) {
        sum += w[i - 1];
        left[i] = left[i - 1] + sum * abs(x[i] - x[i - 1]);
    }
    sum = 0;
    for(int i = n - 2; i >= 0; -- i) {
        sum += w[i + 1];
        right[i] = right[i + 1] + sum * abs(x[i + 1] - x[i]);
    }
    static int dp[2020];
    rep(i, 2020) dp[i] = INF;
    dp[0] = 0;
    for(int i = 0; i < n; ++ i) {
        for(int j = i + 1; j <= n; ++ j) {
            int ok = i, ng = j;
            while(ng - ok > 1) {
                int mid = (ok + ng) / 2;
                if(lsum[mid] - lsum[i] < lsum[j] - lsum[mid]) ok = mid;
                else ng = mid;
            }
            int ss = left[ok] - left[i] - (x[ok] - x[i]) * lsum[i];
            ss += right[ok] - right[j - 1] - (x[j - 1] - x[ok]) * rsum[j];
            chmin(dp[j],dp[i] + ss + 1 + k + x[j - 1] - x[i]);
        }
    }
    cout << dp[n] << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3348
final_rank: 3

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

struct absolute_minima{
	using ll = long long;
	
	priority_queue<pair<ll,ll>> pQ;
	priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> sQ;
	ll pSum=0,sSum=0,pCnt=0,sCnt=0;
	
	void add(long long x,long long w){
		sQ.emplace(x,w);
		sSum += x*w;
		sCnt += w;
		while(true){
			if(pQ.size()>0&&sQ.size()>0){
				if(pQ.top().first>sQ.top().first){
					move(pCnt > sCnt);
					continue;
				}
				else if(pCnt > sCnt){
					move(true);
					continue;
				}
				else if(pCnt + sQ.top().second <= sCnt - sQ.top().second){
					move(false);
					continue;
				}
			}
			else{
				if(sQ.size()==0){
					move(true);
					continue;
				}
				else{
					if(sQ.top().second <= sCnt - sQ.top().second){
						move(false);
						continue;
					}
				}
			}
			break;
		}
	}
	
	void add(ll x){
		add(x,1LL);
	}
	
	ll median(){
		return sQ.top().first;
	}
	
	vector<ll> medians(){
		vector<ll> ret;
		if((pCnt+sCnt)%2==0){
			if(pCnt==sCnt)ret.push_back(pQ.top().first);
			else ret.push_back(sQ.top().first);
		}
		ret.push_back(median());
		return ret;
	}
	
	ll distance_sum(){
		ll ret = 0LL;
		ret -= pSum;
		ret += sSum;
		ret += pCnt * median();
		ret -= sCnt * median();
		return ret;
	}
	
	void move(bool f){
		if(f){
			if(pQ.size()>0){
				pair<ll,ll> p = pQ.top();
				pQ.pop();
				pSum -= p.first*p.second;
				pCnt -= p.second;
				sQ.push(p);
				sSum += p.first*p.second;
				sCnt += p.second;
			}
		}
		else{
			if(sQ.size()>0){
				pair<ll,ll> p = sQ.top();
				sQ.pop();
				sSum -= p.first*p.second;
				sCnt -= p.second;
				pQ.push(p);
				pSum += p.first*p.second;
				pCnt += p.second;
			}
		}
	}
	
};

int main(){
	
	int N;
	cin>>N;
	long long K;
	cin>>K;
	
	vector<long long> X(N),W(N);
	rep(i,N){
		cin>>X[i]>>W[i];
	}
	
	vector<long long> dp(N+1,Inf);
	dp[0] = 0LL;
	
	rep(i,N){
		absolute_minima A;
		for(int j=i+1;j>=1;j--){
			A.add(X[j-1],W[j-1]);
			dp[i+1] = min(dp[i+1],1LL + A.distance_sum() + dp[j-1] + K  + abs(X[i]-A.median()) + abs(X[j-1]-A.median()));
		}
	}
	
	cout<<dp.back()<<endl;
	
    return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 3544
final_rank: 14

code_snippet: #pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("unroll-loops")
//#pragma warning(disable : 4996)

#ifdef _MSC_VER
#include <intrin.h>

#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#endif

#include <limits.h>
#include <math.h>
#include <time.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

//#include<atcoder/all>

using namespace std;

//using namespace atcoder;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define print(x) cout << (x) << endl
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
#define int long long
//#define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v) REP(i, v.size()) cout << v[i] << " \n"[i==(int)v.size()-1]
template <typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template <typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 500050;
const double pi = acos(-1);
constexpr double EPS = 1e-8;
constexpr ll LINF = 1e18 + 1;
constexpr int INF = 1e9 + 1;
void Yes(bool cond) { cout << (cond ? "Yes" : "No") << '\n'; }
void YES(bool cond) { cout << (cond ? "YES" : "NO") << '\n'; }



int X[2020], W[2020];
int ccl[2020], wcl[2020];//[0,i)
int ccr[2020], wcr[2020];//[i,N)
ll dp[2020];
int N, K;
ll f(int l, int r) {
	int sum = wcl[r+1] - wcl[l];

	int ok = r, ng = l-1;
	while (abs(ok - ng) > 1) {
		int mid = (ok + ng) / 2;
		if (wcl[mid+1] - wcl[l] >= sum / 2)ok = mid;
		else ng = mid;
	}
	//DEBUG(l);
	//DEBUG(r);
	//DEBUG(sum);
	//DEBUG(ok);
	//DEBUG(ccl[ok]);
	//DEBUG(ccl[l]);
	//DEBUG(ccr[ok]);
	//DEBUG(ccr[r]);
	ll lc = ccl[ok] - ccl[l] - (wcl[l] * (X[ok] - X[l]));
	ll rc = ccr[ok] - ccr[r] - wcr[r+1] * (X[r] - X[ok]);
	//DEBUG(lc);
	//DEBUG(rc);
	return  lc+rc + X[r] - X[l] + K;
}
void solve() {
	cin >> N >> K;
	REP(i, N) {
		cin >> X[i] >> W[i];
	}
	X[N] = LINF;
	W[N] = LINF;
	REP(i, N) {
		wcl[i + 1] =wcl[i] + W[i];
		ccl[i + 1] =ccl[i]+wcl[i+1] * (X[i + 1] - X[i]);
	}
	REPR(i, N) {
		wcr[i] = wcr[i + 1] + W[i];
		if (i < N - 1)ccr[i] = ccr[i + 1] + wcr[i + 1] * (X[i + 1] - X[i]);
	}
	REP(i, N + 1)dp[i] = LINF;
	dp[0] = 0;
	REP(i, N) {
		FOR(j, i, N + 1) {
			int res = f(i, j);
			//cerr << i << " " << j << " " << res<< endl;
		}
	}
	REP(i, N) {
		REP(j, i + 1) {
			chmin(dp[i + 1], dp[j] + f(j, i)+1);
		}
	}
	print(dp[N]);
}



signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	//int q;
	//cin >> q;
	//while (q--)
	solve();

}
accuracy: 1
time_ms: 130
memory_kb: 3368
final_rank: 12