/*
ID: USACO_template
LANG: C++
PROG: http://web.pdf.com/~pdfastest/fae/pdFT_Overview/pdft.html
*/
#include <iostream> //cin , cout
#include <fstream> //fin, fout
#include <stdio.h> // scanf , pringf
#include <cstdio>
#include <algorithm> // sort , stuff
#include <stack> // stacks
#include <queue> // queues
#include <map>
#include <string>
#include <string.h>
#include <set>
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi; /// adjlist without weight
typedef vector<pii> vii; /// adjlist with weight
typedef vector<pair<int,pii>> vpip; /// edge with weight
typedef long long ll;
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define sz(x) (int)(x).size()
#define adjread {int a, b; cin >> a >> b; adjlist[a].pb(b); adjlist[b].pb(a); }
const int MOD = 1e9+7; // 998244353;
const int MX = 2e5+5; //
const ll INF = 1e18; //
#define MAXV 300007
#define MAXE 100007
const int xdir[4] = {1,0,-1,0}, ydir[4] = {0,1,0,-1}; /// 4 directions
struct NODE {
int x, y;
int val;
int visited;
bool operator< (NODE b) const { return (x == b.x) ? (y < b.y) : (x < b.x); }
};
struct EDGE {
int from, to;
ll weight;
bool operator<(EDGE other) const { return weight < other.weight; }
};
bool debug;
int N, K;
ll ps[MAXV];
/**
* dp[i] = max (1 <=j <= j' <=i) { dp[j] + ps[i] - ps[j'] } = max { dp[j] - ps[j'] } + ps[i]
* instead of doing O(N^2) DP, we use proximation
* for each step, dp[i] = previous best { dp[j] - ps[j] } + ps[i] - err, where err is the proximation to optimal dp
* and update previous best { } if dp[i]-ps[i] is better
*/
pair<ll, ll> dp[MAXV];
pair<ll, ll> canDo(ll err) {
pair<ll, ll> bestDP = mp(0LL, 0LL);
dp[0] = bestDP;
for(int i=1; i<=N; i++) {
dp[i] = mp(bestDP.ff + ps[i] - err, bestDP.ss + 1LL);
dp[i] = max(dp[i], dp[i-1]);
if(dp[i].ff - ps[i] > bestDP.ff) {
bestDP = mp(dp[i].ff - ps[i], dp[i].ss);
} else if(dp[i].ff - ps[i] == bestDP.ff && bestDP.ss > dp[i].ss) {
bestDP = mp(dp[i].ff - ps[i], dp[i].ss);
}
}
return dp[N];
}
int main() {
debug = false;
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N >> K;
for(int i = 1; i <=N; i++) {
ll a; cin >> a;
ps[i] = ps[i-1] + a;
}
ll lo = 0LL, hi = INF;
while(lo < hi) {
if(debug) cout << "walk " << lo << " " << hi << endl;
ll mid = (lo + hi) / 2LL;
auto ans = canDo(mid);
if(ans.ss <= K) {
hi = mid;
} else {
lo = mid + 1;
}
}
auto ans = canDo(hi);
cout << ans.ff + (ll)K * hi << endl;
}
/**
6 1
1 -2 3 -1 5 -6
6 2
1 2 3 -10 5 6
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
9884 KB |
Output is correct |
2 |
Correct |
93 ms |
9992 KB |
Output is correct |
3 |
Correct |
98 ms |
10104 KB |
Output is correct |
4 |
Correct |
96 ms |
10052 KB |
Output is correct |
5 |
Correct |
120 ms |
10088 KB |
Output is correct |
6 |
Correct |
129 ms |
9836 KB |
Output is correct |
7 |
Correct |
91 ms |
9924 KB |
Output is correct |
8 |
Correct |
101 ms |
10128 KB |
Output is correct |
9 |
Correct |
118 ms |
9924 KB |
Output is correct |
10 |
Correct |
105 ms |
9960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
8276 KB |
Output is correct |
2 |
Correct |
82 ms |
8468 KB |
Output is correct |
3 |
Correct |
80 ms |
8200 KB |
Output is correct |
4 |
Correct |
81 ms |
8328 KB |
Output is correct |
5 |
Correct |
99 ms |
9920 KB |
Output is correct |
6 |
Correct |
79 ms |
8160 KB |
Output is correct |
7 |
Correct |
85 ms |
8488 KB |
Output is correct |
8 |
Correct |
100 ms |
10052 KB |
Output is correct |
9 |
Correct |
96 ms |
9912 KB |
Output is correct |
10 |
Correct |
82 ms |
8316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
10180 KB |
Output is correct |
2 |
Correct |
113 ms |
10108 KB |
Output is correct |
3 |
Correct |
113 ms |
10188 KB |
Output is correct |
4 |
Correct |
150 ms |
10068 KB |
Output is correct |
5 |
Correct |
113 ms |
10264 KB |
Output is correct |
6 |
Correct |
115 ms |
10272 KB |
Output is correct |
7 |
Correct |
118 ms |
10360 KB |
Output is correct |
8 |
Correct |
134 ms |
10252 KB |
Output is correct |
9 |
Correct |
119 ms |
10340 KB |
Output is correct |
10 |
Correct |
135 ms |
10308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
320 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 |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
320 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 |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
3 ms |
332 KB |
Output is correct |
23 |
Correct |
3 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
332 KB |
Output is correct |
30 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
9884 KB |
Output is correct |
2 |
Correct |
93 ms |
9992 KB |
Output is correct |
3 |
Correct |
98 ms |
10104 KB |
Output is correct |
4 |
Correct |
96 ms |
10052 KB |
Output is correct |
5 |
Correct |
120 ms |
10088 KB |
Output is correct |
6 |
Correct |
129 ms |
9836 KB |
Output is correct |
7 |
Correct |
91 ms |
9924 KB |
Output is correct |
8 |
Correct |
101 ms |
10128 KB |
Output is correct |
9 |
Correct |
118 ms |
9924 KB |
Output is correct |
10 |
Correct |
105 ms |
9960 KB |
Output is correct |
11 |
Correct |
79 ms |
8276 KB |
Output is correct |
12 |
Correct |
82 ms |
8468 KB |
Output is correct |
13 |
Correct |
80 ms |
8200 KB |
Output is correct |
14 |
Correct |
81 ms |
8328 KB |
Output is correct |
15 |
Correct |
99 ms |
9920 KB |
Output is correct |
16 |
Correct |
79 ms |
8160 KB |
Output is correct |
17 |
Correct |
85 ms |
8488 KB |
Output is correct |
18 |
Correct |
100 ms |
10052 KB |
Output is correct |
19 |
Correct |
96 ms |
9912 KB |
Output is correct |
20 |
Correct |
82 ms |
8316 KB |
Output is correct |
21 |
Correct |
123 ms |
10180 KB |
Output is correct |
22 |
Correct |
113 ms |
10108 KB |
Output is correct |
23 |
Correct |
113 ms |
10188 KB |
Output is correct |
24 |
Correct |
150 ms |
10068 KB |
Output is correct |
25 |
Correct |
113 ms |
10264 KB |
Output is correct |
26 |
Correct |
115 ms |
10272 KB |
Output is correct |
27 |
Correct |
118 ms |
10360 KB |
Output is correct |
28 |
Correct |
134 ms |
10252 KB |
Output is correct |
29 |
Correct |
119 ms |
10340 KB |
Output is correct |
30 |
Correct |
135 ms |
10308 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
320 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
324 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Correct |
2 ms |
332 KB |
Output is correct |
52 |
Correct |
3 ms |
332 KB |
Output is correct |
53 |
Correct |
3 ms |
332 KB |
Output is correct |
54 |
Correct |
2 ms |
332 KB |
Output is correct |
55 |
Correct |
2 ms |
332 KB |
Output is correct |
56 |
Correct |
2 ms |
332 KB |
Output is correct |
57 |
Correct |
2 ms |
332 KB |
Output is correct |
58 |
Correct |
2 ms |
332 KB |
Output is correct |
59 |
Correct |
2 ms |
332 KB |
Output is correct |
60 |
Correct |
2 ms |
332 KB |
Output is correct |
61 |
Correct |
157 ms |
10200 KB |
Output is correct |
62 |
Correct |
182 ms |
10316 KB |
Output is correct |
63 |
Correct |
134 ms |
10128 KB |
Output is correct |
64 |
Correct |
165 ms |
10308 KB |
Output is correct |
65 |
Correct |
165 ms |
10308 KB |
Output is correct |
66 |
Correct |
168 ms |
10380 KB |
Output is correct |
67 |
Correct |
161 ms |
10040 KB |
Output is correct |
68 |
Correct |
117 ms |
10336 KB |
Output is correct |
69 |
Correct |
126 ms |
10140 KB |
Output is correct |
70 |
Correct |
113 ms |
10004 KB |
Output is correct |