#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define all(a) a.begin(),a.end()
#define sz(a) (int)(a.size())
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
int main()
{
owo
int n;
cin>>n;
ll d = 0;
priority_queue<ll>q;
q.push(0);
vector<int>a(n),b(n);
for(int i=0;i<n;i++)cin>>a[i]>>b[i];
ll p = 0; //dp value of the optimum point
for(int i=0;i<n;i++){
d+=a[i] - b[i];
ll dt = d;
if(d<0){
p-=dt;
dt=0;
}
if(dt > q.top())q.push(dt);
else{
p+=q.top() - dt;
q.pop();
q.push(dt);
q.push(dt);
}
}
ll m = 0;
//we want the dp value at point d
//y = mx+c;
//y = (m-1)x + c1
//c1 = c + x
//c here is just p so we pop and add until we reach the segment that contains d
while(q.top() > d){
m--;
p+=q.top();
q.pop();
}
cout<<d*m + p;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
16 ms |
1616 KB |
Output is correct |
5 |
Correct |
35 ms |
2892 KB |
Output is correct |
6 |
Correct |
116 ms |
7780 KB |
Output is correct |
7 |
Correct |
149 ms |
15208 KB |
Output is correct |
8 |
Correct |
186 ms |
13288 KB |
Output is correct |
9 |
Correct |
159 ms |
12628 KB |
Output is correct |
10 |
Correct |
116 ms |
10416 KB |
Output is correct |
11 |
Correct |
125 ms |
10352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
16 ms |
1616 KB |
Output is correct |
5 |
Correct |
35 ms |
2892 KB |
Output is correct |
6 |
Correct |
116 ms |
7780 KB |
Output is correct |
7 |
Correct |
149 ms |
15208 KB |
Output is correct |
8 |
Correct |
186 ms |
13288 KB |
Output is correct |
9 |
Correct |
159 ms |
12628 KB |
Output is correct |
10 |
Correct |
116 ms |
10416 KB |
Output is correct |
11 |
Correct |
125 ms |
10352 KB |
Output is correct |
12 |
Correct |
52 ms |
4108 KB |
Output is correct |
13 |
Correct |
136 ms |
10832 KB |
Output is correct |
14 |
Correct |
149 ms |
15212 KB |
Output is correct |
15 |
Correct |
181 ms |
13284 KB |
Output is correct |
16 |
Correct |
212 ms |
12644 KB |
Output is correct |
17 |
Correct |
111 ms |
10356 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
16 ms |
1616 KB |
Output is correct |
12 |
Correct |
35 ms |
2892 KB |
Output is correct |
13 |
Correct |
116 ms |
7780 KB |
Output is correct |
14 |
Correct |
149 ms |
15208 KB |
Output is correct |
15 |
Correct |
186 ms |
13288 KB |
Output is correct |
16 |
Correct |
159 ms |
12628 KB |
Output is correct |
17 |
Correct |
116 ms |
10416 KB |
Output is correct |
18 |
Correct |
125 ms |
10352 KB |
Output is correct |
19 |
Correct |
52 ms |
4108 KB |
Output is correct |
20 |
Correct |
136 ms |
10832 KB |
Output is correct |
21 |
Correct |
149 ms |
15212 KB |
Output is correct |
22 |
Correct |
181 ms |
13284 KB |
Output is correct |
23 |
Correct |
212 ms |
12644 KB |
Output is correct |
24 |
Correct |
111 ms |
10356 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
328 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
42 ms |
4052 KB |
Output is correct |
34 |
Correct |
105 ms |
10848 KB |
Output is correct |
35 |
Correct |
178 ms |
15080 KB |
Output is correct |
36 |
Correct |
163 ms |
12732 KB |
Output is correct |
37 |
Correct |
154 ms |
13288 KB |
Output is correct |
38 |
Correct |
158 ms |
15164 KB |
Output is correct |
39 |
Correct |
117 ms |
11376 KB |
Output is correct |
40 |
Correct |
119 ms |
10812 KB |
Output is correct |
41 |
Correct |
114 ms |
10344 KB |
Output is correct |
42 |
Correct |
112 ms |
10344 KB |
Output is correct |
43 |
Correct |
107 ms |
10396 KB |
Output is correct |
44 |
Correct |
108 ms |
10356 KB |
Output is correct |
45 |
Correct |
198 ms |
15088 KB |
Output is correct |
46 |
Correct |
134 ms |
10684 KB |
Output is correct |
47 |
Correct |
114 ms |
10268 KB |
Output is correct |