#include using namespace std; #define REP(i, s) for (int i = 0; i < s; ++i) #define ALL(v) (v).begin(), (v).end() #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl #define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i) #define DEBUG #define int long long #define INF 1e18 template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template ostream& operator << (ostream &s, pair P) { return s << '<' << P.first << ", " << P.second << '>'; } template ostream& operator << (ostream &s, vector P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template ostream& operator << (ostream &s, vector > P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } template ostream& operator << (ostream &s, set P) { EACH(it, P) { s << "<" << *it << "> "; } return s << endl; } template ostream& operator << (ostream &s, map P) { EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; } templatevoid show(vectorv){for (int i = 0; i < v.size(); i++){cerr<>n>>q; vector>v(n); REP(i,n) { cin>>v[i].first>>v[i].second; } sort(ALL(v)); vectord(n); REP(i,n) d[i]=v[i].first; vectorrui(n+1,0); for (int i = 1; i <= n; i++) { rui[i]=rui[i-1]+v[i-1].second; } //show(rui); int c3 = v[n/2].first; int c1=v[0].first; int c2=v[n-1].first; int cnt1=0; for (int i = 0; i < n; i++) { cnt1+=v[i].second*(v[i].first - c1); } int cnt2 = 0; for (int i = 0; i < n; i++) { cnt2 += v[i].second * abs(v[i].first - c2); } int cnt3=0; for (int i = 0; i < n; i++) { cnt3 += v[i].second * abs(v[i].first - c3); } // cerr<>x; if(x<=c1){ cout << (rui[n] - rui[0]) * abs(x - c1) + cnt1 << endl; } else if (x>=c2){ cout<<(rui[n]-rui[0])*(x-c2)+ cnt2<