0%

[題解]物品堆疊

a140. 物品堆疊

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define double long double
#define N 100005
#define Orz ios::sync_with_stdio(0),cin.tie(0)
#define INF 2e18
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define rrep(i,l,r) for(int i=l;i<r;i++)
#define pii pair<int,int>
#define x first
#define y second
using namespace std;
int n;
pii p[N];

bool cmp(pii a, pii b){
return a.x*b.y < a.y*b.x;
//p[1~n]變成從上到下
}

signed main(){
Orz;
cin>>n;
rep(i,0,n-1)cin>>p[i].x;
rep(i,0,n-1)cin>>p[i].y;
sort(p,p+n,cmp);
int weight = 0,ans = 0;
for(int i=0;i<n-1;i++){
weight += p[i].x;
ans += weight*p[i+1].y;
}
cout<<ans<<endl;
}