1
+ #include < cstdio>
2
+ #include < iostream>
3
+ #include < cstring>
4
+ #include < cmath>
5
+ #include < ctime>
6
+ #include < vector>
7
+ #include < string>
8
+ #include < algorithm>
9
+ #include < map>
10
+ #include < set>
11
+ #include < unordered_map>
12
+ #include < unordered_set>
13
+ #include < queue>
14
+ #include < deque>
15
+ #include < stack>
16
+ #include < numeric>
17
+ #include < memory>
18
+ #include < list>
19
+ #include < climits>
20
+ #include < fstream>
21
+ #include < sstream>
22
+ #include < random>
23
+ #include < functional>
24
+
25
+ #define PB push_back
26
+ #define F first
27
+ #define S second
28
+
29
+ #define REP (i,from,to ) for (auto i=(from); i<=(to); ++i)
30
+ #define PER (i,from,to ) for (auto i=(from); i>=(to); --i)
31
+ #define REP_IF (i,from,to,assert ) for (auto i=(from); i<=(to); ++i) if (assert)
32
+
33
+ #define FOR (i,less_than ) for (auto i=0 ; i<(less_than); ++i)
34
+ #define FORI (i, container ) for (auto i=0 ; i<(container).size(); ++i)
35
+ #define FORI_IF (i, container, assert ) for (auto i=0 ; i<(container).size(); ++i) if (assert)
36
+ #define ROFI (i, container ) for (auto i=SZ(container)-1 ; i>=0 ; --i)
37
+
38
+ #define FOREACH (elem, container ) for (auto elem : (container))
39
+ #define MEMSET (container, value ) memset(container, value, sizeof (container))
40
+ #define MEMSET0 (container ) memset(container, 0 , sizeof (container))
41
+ #define FILL (container, value ) fill(container.begin(), container.end(), value)
42
+ #define FILL0 (container ) fill(container.begin(), container.end(), 0 )
43
+ #define ALL (container ) (container).begin(), (container).end()
44
+ #define SZ (container ) (int )((container).size())
45
+
46
+ #define BACK (set_map ) *prev ((set_map).end(), 1)
47
+ #define FRONT (set_map ) *(set_map).begin()
48
+
49
+ #define POP (var, container ) auto var=(container.front()); container.pop();
50
+
51
+ using PII = std::pair<int ,int >;
52
+ using LL = long long ;
53
+ using VI = std::vector<int >;
54
+ using CVI = const VI;
55
+ using VLL = std::vector<LL>;
56
+ using VVI = std::vector<VI>;
57
+ using VVLL = std::vector<VLL>;
58
+
59
+ using namespace std ;
60
+
61
+
62
+ int main () {
63
+ int n;
64
+ cin >> n;
65
+ vector<pair<LL,LL>> a (n+1 );
66
+ REP (i, 1 , n) {
67
+ cin >> a[i].F >> a[i].S ;
68
+ }
69
+ sort (a.begin ()+1 , a.end (), [](const PII& a, const PII& b) {
70
+ return a.F -a.S > b.F -b.S ;
71
+ });
72
+
73
+ LL ans = 0 ;
74
+ REP (i, 1 , n)
75
+ ans += (a[i].F -a[i].S )*i + a[i].S *n - a[i].F ;
76
+ cout << ans << endl;
77
+ return 0 ;
78
+ }
0 commit comments