//============================================================================
// Name : mmmmmmmm.cpp
// Author : sameh mohamed
// Copyright : don't try i'm warnning you......
// Description : Hello World
//============================================================================
//Egypt came then History
//Pharoas were coders but using different compilers{}{}{}{}{}}}}{{{{@#$%^^&&$$$###()()(***)))([][][][][]]][
#include<bits/stdc++.h>
#include <iostream>
#include<string>
#include<algorithm>
#include<set>
#include<vector>
#include<map>
#include<cmath>
using namespace std;
long long minc=1e9;
int main()
{
int n;
long long d;
cin>>n>>d;
vector<long long>a(n),b(n);
for(int i=0;i<n;i++)
{
cin>>a[i]>>b[i];
long long m=min(a[i],b[i]);
minc=min(minc,m);
}
long long w=minc+minc+1+2*d;
long long s=0;
for(int i=0;i<n;i++)
{
long long x;
long long h=min(a[i],b[i]);
if(h!=minc)
x=h+d+minc;
else
x=a[i]+b[i];
long long m=min(a[i]+b[i],x);
m=min(w,m);
s+=m;
}
cout<<s;
return 0;
}
Copy