# MooFest(树状数组+离线处理)

/*

*/
#if 0
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXX=20000+100;
int c[3][MAXX];
int lowbit(int x)
{
return x&(-x);
}
{
for(; x<MAXX; x+=lowbit(x))
{
c[p][x]+=v;
}
}
int sum(int p,int x)
{
int sum=0;
for(; x>0; x-=lowbit(x))
{
sum+=c[p][x];
}
return sum;
}
struct node
{
int x,v;
bool operator <(const node& b)const
{
return v<b.v;
}
}nodes[MAXX];

int main()
{
ios::sync_with_stdio(false);
int n;
while(cin>>n&&n)
{
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++)
{
cin>>nodes[i].v>>nodes[i].x;
}
sort(nodes+1,nodes+n+1);

int total=0;
long long  end=0;
for(int i=1;i<=n;i++)
{
int x = nodes[i].x;
total+=x;
int s1=sum(1,x);//左边有多少个数
int s2=sum(2,x);//左边数的位置的和
int temp1=s1*x-s2;//左边的坐标差
int temp2=total-s2-x*(i- s1);//右边的坐标差
end+=((long long)temp1+temp2)*nodes[i].v;
}
cout<<end<<endl;
}
return 0;

}
#endif

﻿﻿
Problem Description

Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a social gathering of cows from around the world. MooFest involves a variety of events including haybale stacking, fence jumping, pin the tail on the farmer, and of course, mooing. When the cows all stand in line for a particular event, they moo so loudly that the roar is practically deafening. After participating in this event year after year, some of the cows have in fact lost a bit of their hearing.

Each cow i has an associated "hearing" threshold v(i) (in the range 1..20,000). If a cow moos to cow i, she must use a volume of at least v(i) times the distance between the two cows in order to be heard by cow i. If two cows i and j wish to converse, they must speak at a volume level equal to the distance between them times max(v(i),v(j)).

Suppose each of the N cows is standing in a straight line (each cow at some unique x coordinate in the range 1..20,000), and every pair of cows is carrying on a conversation using the smallest possible volume.

Compute the sum of all the volumes produced by all N(N-1)/2 pairs of mooing cows.

Input

* Line 1: A single integer, N <br> <br>* Lines 2..N+1: Two integers: the volume threshold and x coordinate for a cow. Line 2 represents the first cow; line 3 represents the second cow; and so on. No two cows will stand at the same location. <br>

Output

* Line 1: A single line with a single integer that is the sum of all the volumes of the conversing cows. <br>

Sample Input

4 3 1 2 5 2 6 4 3

Sample Output

57