-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear.cpp
More file actions
98 lines (97 loc) · 2.17 KB
/
linear.cpp
File metadata and controls
98 lines (97 loc) · 2.17 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <bits/stdc++.h>
using namespace std;
#define int_m 2147483648
struct line
{
long long a,b,s,e;
bool operator <(const line &g)
{
if(a==g.a)
{
return b>g.b;
}
return a<g.a;
}
};
void clear(stack<line> &f)
{
stack<line> eap;
swap(eap,f);
}
int main()
{
int n,m;
long long *x;
line *f;
stack<line> w;
while(scanf("%d%d",&n,&m)&&(n))
{
f=new line[n];
x=new long long[m];
for(int i=0;i<n;i++)
{
scanf("%lld%lld",&f[i].a,&f[i].b);
}
for(int i=0;i<m;i++)
{
scanf("%lld",&x[i]);
}
sort(f,f+n);
sort(x,x+m,greater<int>());
f[0].s=-int_m;
w.push(f[0]);
long long last=f[0].a;
for(int i=1;i<n;i++)
{
if(f[i].a==last)
continue;
last=f[i].a;
if(w.size()==1)
{
w.top().e=(f[i].b-w.top().b)/(w.top().a-f[i].a);
f[i].s=w.top().e+1;
w.push(f[i]);
}
else
{
long long ns;
while(1)
{
ns=(w.top().b-f[i].b)/(f[i].a-w.top().a)+1;
if(ns<=w.top().s)
{
w.pop();
}
else
break;
}
f[i].s=ns;
w.top().e=ns-1;
w.push(f[i]);
}
}
w.top().e=int_m;
long long ans(0);
line lst=w.top();
w.pop();
for(int i=0;i<m;i++)
{
while(!w.empty())
{
if(x[i]*lst.a+lst.b<x[i]*w.top().a+w.top().b)
{
lst=w.top();
w.pop();
}
else
break;
}
ans+=(lst.a*x[i]+lst.b);
}
cout<<ans<<endl;
delete []f;
delete []x;
clear(w);
}
return 0;
}