Submission #843606

# Submission time Handle Problem Language Result Execution time Memory
843606 2023-09-04T08:25:54 Z fanwen Feast (NOI19_feast) C++17
100 / 100
123 ms 9716 KB
#include <bits/stdc++.h>

using namespace std;

#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define debug(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }

template <class A, class B> bool minimize(A &a, B b)  { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b)  { if (a < b) { a = b; return true; } return false; }

const int MAXN = 3e5 + 5;

int N, K, cnt[MAXN];
long long a[MAXN], dp[MAXN];

bool check(long long val) {
	pair <long long, int> best(0, 0);
	FOR(i, 1, N) {
		dp[i] = dp[i - 1], cnt[i] = cnt[i - 1];
		if(maximize(dp[i], best.first + a[i] - val)) {
			cnt[i] = cnt[best.second] + 1;
		}

		if(maximize(best.first, dp[i] - a[i])) {
			best.second = i;
		}
	}
	return cnt[N] <= K;
}

void you_make_it(void) {

    cin >> N >> K;
    FOR(i, 1, N) cin >> a[i], a[i] += a[i - 1];
    long long L = -1, R = 1E18;
    while(R - L > 1) {
    	auto Mid = L + R >> 1LL;
    	if(check(Mid)) R = Mid;
    	else L = Mid;
    }
    check(R);
    cout << dp[N] + 1LL * cnt[N] * R;
}

signed main() {

#ifdef LOCAL
    freopen("TASK.inp", "r", stdin);
    freopen("TASK.out", "w", stdout);
#endif
    auto start_time = chrono::steady_clock::now();

    cin.tie(0), cout.tie(0) -> sync_with_stdio(0);

    you_make_it();

    auto end_time = chrono::steady_clock::now();

    cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;

    return (0 ^ 0);
}

// Dream it. Wish it. Do it.

Compilation message

feast.cpp: In function 'void you_make_it()':
feast.cpp:44:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   44 |      auto Mid = L + R >> 1LL;
      |                 ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 64 ms 5976 KB Output is correct
2 Correct 61 ms 6224 KB Output is correct
3 Correct 57 ms 6224 KB Output is correct
4 Correct 62 ms 6284 KB Output is correct
5 Correct 60 ms 6268 KB Output is correct
6 Correct 50 ms 5972 KB Output is correct
7 Correct 51 ms 5976 KB Output is correct
8 Correct 60 ms 6224 KB Output is correct
9 Correct 62 ms 5976 KB Output is correct
10 Correct 62 ms 6044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 6236 KB Output is correct
2 Correct 53 ms 6092 KB Output is correct
3 Correct 59 ms 5972 KB Output is correct
4 Correct 47 ms 6232 KB Output is correct
5 Correct 54 ms 6232 KB Output is correct
6 Correct 51 ms 7336 KB Output is correct
7 Correct 52 ms 7284 KB Output is correct
8 Correct 63 ms 9176 KB Output is correct
9 Correct 55 ms 9408 KB Output is correct
10 Correct 58 ms 7252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 6228 KB Output is correct
2 Correct 84 ms 6224 KB Output is correct
3 Correct 84 ms 9052 KB Output is correct
4 Correct 82 ms 9464 KB Output is correct
5 Correct 85 ms 9044 KB Output is correct
6 Correct 87 ms 9320 KB Output is correct
7 Correct 83 ms 9308 KB Output is correct
8 Correct 85 ms 9556 KB Output is correct
9 Correct 93 ms 9404 KB Output is correct
10 Correct 89 ms 9140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2516 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2516 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2552 KB Output is correct
11 Correct 1 ms 2512 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2516 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2516 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2552 KB Output is correct
11 Correct 1 ms 2512 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2516 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2852 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2540 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2396 KB Output is correct
30 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 5976 KB Output is correct
2 Correct 61 ms 6224 KB Output is correct
3 Correct 57 ms 6224 KB Output is correct
4 Correct 62 ms 6284 KB Output is correct
5 Correct 60 ms 6268 KB Output is correct
6 Correct 50 ms 5972 KB Output is correct
7 Correct 51 ms 5976 KB Output is correct
8 Correct 60 ms 6224 KB Output is correct
9 Correct 62 ms 5976 KB Output is correct
10 Correct 62 ms 6044 KB Output is correct
11 Correct 52 ms 6236 KB Output is correct
12 Correct 53 ms 6092 KB Output is correct
13 Correct 59 ms 5972 KB Output is correct
14 Correct 47 ms 6232 KB Output is correct
15 Correct 54 ms 6232 KB Output is correct
16 Correct 51 ms 7336 KB Output is correct
17 Correct 52 ms 7284 KB Output is correct
18 Correct 63 ms 9176 KB Output is correct
19 Correct 55 ms 9408 KB Output is correct
20 Correct 58 ms 7252 KB Output is correct
21 Correct 81 ms 6228 KB Output is correct
22 Correct 84 ms 6224 KB Output is correct
23 Correct 84 ms 9052 KB Output is correct
24 Correct 82 ms 9464 KB Output is correct
25 Correct 85 ms 9044 KB Output is correct
26 Correct 87 ms 9320 KB Output is correct
27 Correct 83 ms 9308 KB Output is correct
28 Correct 85 ms 9556 KB Output is correct
29 Correct 93 ms 9404 KB Output is correct
30 Correct 89 ms 9140 KB Output is correct
31 Correct 1 ms 2392 KB Output is correct
32 Correct 1 ms 2396 KB Output is correct
33 Correct 1 ms 2396 KB Output is correct
34 Correct 1 ms 2396 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
36 Correct 1 ms 2396 KB Output is correct
37 Correct 1 ms 2516 KB Output is correct
38 Correct 1 ms 2396 KB Output is correct
39 Correct 1 ms 2396 KB Output is correct
40 Correct 1 ms 2552 KB Output is correct
41 Correct 1 ms 2512 KB Output is correct
42 Correct 1 ms 2652 KB Output is correct
43 Correct 1 ms 2652 KB Output is correct
44 Correct 1 ms 2396 KB Output is correct
45 Correct 1 ms 2516 KB Output is correct
46 Correct 1 ms 2396 KB Output is correct
47 Correct 1 ms 2396 KB Output is correct
48 Correct 1 ms 2396 KB Output is correct
49 Correct 1 ms 2396 KB Output is correct
50 Correct 1 ms 2652 KB Output is correct
51 Correct 1 ms 2652 KB Output is correct
52 Correct 1 ms 2652 KB Output is correct
53 Correct 1 ms 2396 KB Output is correct
54 Correct 1 ms 2852 KB Output is correct
55 Correct 1 ms 2396 KB Output is correct
56 Correct 1 ms 2540 KB Output is correct
57 Correct 1 ms 2396 KB Output is correct
58 Correct 1 ms 2652 KB Output is correct
59 Correct 1 ms 2396 KB Output is correct
60 Correct 1 ms 2396 KB Output is correct
61 Correct 114 ms 9224 KB Output is correct
62 Correct 106 ms 9300 KB Output is correct
63 Correct 103 ms 9020 KB Output is correct
64 Correct 113 ms 9716 KB Output is correct
65 Correct 123 ms 9300 KB Output is correct
66 Correct 112 ms 9300 KB Output is correct
67 Correct 122 ms 9140 KB Output is correct
68 Correct 83 ms 9324 KB Output is correct
69 Correct 86 ms 9164 KB Output is correct
70 Correct 81 ms 9300 KB Output is correct