#pragma GCC optimize("Ofast,inline")
#pragma GCC optimize ("unroll-loops")
#include <iostream>
#include <cmath>
#include <unordered_map>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
#include <chrono>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
typedef long long ll;
ll linf = 1e15+1;
inline void scoobydoobydoo(){
ios::sync_with_stdio(false);
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
}
const int MAXN = 3e5+3;
ll arr[MAXN];
pair<long double, ll> mem[MAXN][2];
ll n, k;
bool isVisited[MAXN][2];
long double lambda;
pair<long double, ll> dp(){
mem[0][0] = {0, 0};
mem[0][1] = {arr[0]-lambda, 1};
for (int i = 1; i < n; i++){
// 0
mem[i][0] = max(mem[i-1][0], mem[i-1][1]);
//buy from 0, continue 1
mem[i][1] = max(make_pair(mem[i-1][0].first+arr[i]-lambda, mem[i-1][0].second+1), make_pair(mem[i-1][1].first+arr[i], mem[i-1][1].second));
}
return max(mem[n-1][0], mem[n-1][1]);
}
bool check(long double la){
lambda = la;
for (int i = 0; i < n; i++){
isVisited[i][0] = false;
isVisited[i][1] = false;
}
//cout << la << " " << dp(0, 0).first << " " << dp(0, 0).second << endl;
return dp().second >= k;
}
int main(){
scoobydoobydoo();
cin >> n >> k;
for (int i = 0; i < n; i++)cin >> arr[i];
long double b = 0;
for (int i = 0; i < n; i++)b += abs(arr[i]);
long double a = 0;
chrono::time_point<chrono::system_clock> start, end;
start = chrono::system_clock::now();
while (a+(1e-3) < b){
long double mid = (a+b)/2.0;
if (check(mid)){
a = mid;
} else {
b = mid;
}
end = chrono::system_clock::now();
chrono::duration<double> elapsed_seconds = end - start;
if (elapsed_seconds.count() > 0.9)break;
}
check(a);
ll ans = round(a*k + max(mem[n-1][0], mem[n-1][1]).first);
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
147 ms |
22084 KB |
Output is correct |
2 |
Correct |
145 ms |
22108 KB |
Output is correct |
3 |
Correct |
144 ms |
22096 KB |
Output is correct |
4 |
Correct |
135 ms |
22144 KB |
Output is correct |
5 |
Correct |
156 ms |
21960 KB |
Output is correct |
6 |
Correct |
147 ms |
22096 KB |
Output is correct |
7 |
Correct |
138 ms |
22088 KB |
Output is correct |
8 |
Correct |
147 ms |
22136 KB |
Output is correct |
9 |
Correct |
163 ms |
22104 KB |
Output is correct |
10 |
Correct |
147 ms |
22100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
22104 KB |
Output is correct |
2 |
Correct |
99 ms |
22156 KB |
Output is correct |
3 |
Correct |
94 ms |
22108 KB |
Output is correct |
4 |
Correct |
100 ms |
22120 KB |
Output is correct |
5 |
Correct |
137 ms |
21844 KB |
Output is correct |
6 |
Correct |
105 ms |
22104 KB |
Output is correct |
7 |
Correct |
97 ms |
22152 KB |
Output is correct |
8 |
Correct |
158 ms |
22152 KB |
Output is correct |
9 |
Correct |
146 ms |
22084 KB |
Output is correct |
10 |
Correct |
105 ms |
22144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
185 ms |
22056 KB |
Output is correct |
2 |
Correct |
192 ms |
22100 KB |
Output is correct |
3 |
Correct |
196 ms |
22124 KB |
Output is correct |
4 |
Correct |
184 ms |
22104 KB |
Output is correct |
5 |
Correct |
179 ms |
22108 KB |
Output is correct |
6 |
Correct |
195 ms |
22140 KB |
Output is correct |
7 |
Correct |
186 ms |
22144 KB |
Output is correct |
8 |
Correct |
181 ms |
21980 KB |
Output is correct |
9 |
Correct |
206 ms |
22152 KB |
Output is correct |
10 |
Correct |
187 ms |
22152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4508 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4508 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4508 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
2 ms |
4444 KB |
Output is correct |
22 |
Correct |
2 ms |
4444 KB |
Output is correct |
23 |
Correct |
2 ms |
4444 KB |
Output is correct |
24 |
Correct |
2 ms |
4696 KB |
Output is correct |
25 |
Correct |
2 ms |
4568 KB |
Output is correct |
26 |
Correct |
2 ms |
4444 KB |
Output is correct |
27 |
Correct |
2 ms |
4444 KB |
Output is correct |
28 |
Correct |
2 ms |
4444 KB |
Output is correct |
29 |
Correct |
2 ms |
4440 KB |
Output is correct |
30 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
147 ms |
22084 KB |
Output is correct |
2 |
Correct |
145 ms |
22108 KB |
Output is correct |
3 |
Correct |
144 ms |
22096 KB |
Output is correct |
4 |
Correct |
135 ms |
22144 KB |
Output is correct |
5 |
Correct |
156 ms |
21960 KB |
Output is correct |
6 |
Correct |
147 ms |
22096 KB |
Output is correct |
7 |
Correct |
138 ms |
22088 KB |
Output is correct |
8 |
Correct |
147 ms |
22136 KB |
Output is correct |
9 |
Correct |
163 ms |
22104 KB |
Output is correct |
10 |
Correct |
147 ms |
22100 KB |
Output is correct |
11 |
Correct |
98 ms |
22104 KB |
Output is correct |
12 |
Correct |
99 ms |
22156 KB |
Output is correct |
13 |
Correct |
94 ms |
22108 KB |
Output is correct |
14 |
Correct |
100 ms |
22120 KB |
Output is correct |
15 |
Correct |
137 ms |
21844 KB |
Output is correct |
16 |
Correct |
105 ms |
22104 KB |
Output is correct |
17 |
Correct |
97 ms |
22152 KB |
Output is correct |
18 |
Correct |
158 ms |
22152 KB |
Output is correct |
19 |
Correct |
146 ms |
22084 KB |
Output is correct |
20 |
Correct |
105 ms |
22144 KB |
Output is correct |
21 |
Correct |
185 ms |
22056 KB |
Output is correct |
22 |
Correct |
192 ms |
22100 KB |
Output is correct |
23 |
Correct |
196 ms |
22124 KB |
Output is correct |
24 |
Correct |
184 ms |
22104 KB |
Output is correct |
25 |
Correct |
179 ms |
22108 KB |
Output is correct |
26 |
Correct |
195 ms |
22140 KB |
Output is correct |
27 |
Correct |
186 ms |
22144 KB |
Output is correct |
28 |
Correct |
181 ms |
21980 KB |
Output is correct |
29 |
Correct |
206 ms |
22152 KB |
Output is correct |
30 |
Correct |
187 ms |
22152 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4508 KB |
Output is correct |
34 |
Correct |
1 ms |
4444 KB |
Output is correct |
35 |
Correct |
1 ms |
4444 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
4444 KB |
Output is correct |
40 |
Correct |
1 ms |
4444 KB |
Output is correct |
41 |
Correct |
1 ms |
4444 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
1 ms |
4444 KB |
Output is correct |
44 |
Correct |
1 ms |
4444 KB |
Output is correct |
45 |
Correct |
1 ms |
4444 KB |
Output is correct |
46 |
Correct |
1 ms |
4444 KB |
Output is correct |
47 |
Correct |
1 ms |
4444 KB |
Output is correct |
48 |
Correct |
1 ms |
4444 KB |
Output is correct |
49 |
Correct |
1 ms |
4444 KB |
Output is correct |
50 |
Correct |
1 ms |
4444 KB |
Output is correct |
51 |
Correct |
2 ms |
4444 KB |
Output is correct |
52 |
Correct |
2 ms |
4444 KB |
Output is correct |
53 |
Correct |
2 ms |
4444 KB |
Output is correct |
54 |
Correct |
2 ms |
4696 KB |
Output is correct |
55 |
Correct |
2 ms |
4568 KB |
Output is correct |
56 |
Correct |
2 ms |
4444 KB |
Output is correct |
57 |
Correct |
2 ms |
4444 KB |
Output is correct |
58 |
Correct |
2 ms |
4444 KB |
Output is correct |
59 |
Correct |
2 ms |
4440 KB |
Output is correct |
60 |
Correct |
2 ms |
4444 KB |
Output is correct |
61 |
Correct |
241 ms |
22108 KB |
Output is correct |
62 |
Correct |
255 ms |
22152 KB |
Output is correct |
63 |
Correct |
214 ms |
22112 KB |
Output is correct |
64 |
Correct |
249 ms |
22136 KB |
Output is correct |
65 |
Correct |
260 ms |
25180 KB |
Output is correct |
66 |
Correct |
256 ms |
25168 KB |
Output is correct |
67 |
Correct |
261 ms |
25076 KB |
Output is correct |
68 |
Correct |
189 ms |
25100 KB |
Output is correct |
69 |
Correct |
181 ms |
25076 KB |
Output is correct |
70 |
Correct |
182 ms |
25028 KB |
Output is correct |