Submission #856414

# Submission time Handle Problem Language Result Execution time Memory
856414 2023-10-03T12:20:50 Z vjudge1 Feast (NOI19_feast) C++17
100 / 100
187 ms 12580 KB
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")

// #define _GLIBCXX_DEBUG
// #define _GLIBCXX_DEBUG_PEDANTIC

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <array>
#include <numeric>
#include <queue>
#include <deque>
#include <cmath>
#include <climits>
// #include <ext/pb_ds/assoc_container.hpp> 
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/rope>

// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
using namespace std;


const int MOD = 998244353;
const long double PI = 3.141592653589793;
using ll = long long;
const ll INF = 1e18;

#define int ll

// template<typename T>
// using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

// template<typename K, typename V>
// using hash_table = cc_hash_table<K, V, hash<K>>;

template<typename T>
using graph = vector<vector<T>>;

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for (auto& i: a) {
        in >> i;
    }
    return in;
}

template<typename T>
ostream& operator<<(ostream& out, vector<T>& a) {
    for (auto& i: a) {
        out << i << " ";
    }
    return out;
}

ll fast_pow(ll a, ll b, ll mod) {
    if (b == 0)
        return 1;
    if (b % 2) {
        return (1ll * a * fast_pow(a, b - 1, mod)) % mod;
    }
    ll k = fast_pow(a, b / 2, mod);
    return (1ll * k * k) % mod;
}

ll fast_pow(ll a, ll b) {
    if (b == 0)
        return 1;
    if (b % 2) {
        return (1ll * a * fast_pow(a, b - 1));
    }
    ll k = fast_pow(a, b / 2);
    return (1ll * k * k);
}
int n, k;
vector<int> a;

pair<int, int> calc(int L) {
	vector<int> dp(n), c(n);
	int cnt = 0;
	int mx = 0;
	int mx_dp = 0;
	int cnt_dp = 0;

	for (int i = 0; i < n; i++) {
		dp[i] = a[i] - L + mx;
		c[i] = cnt + 1;
		if (mx_dp > dp[i]) {
			dp[i] = mx_dp;
			c[i] = cnt_dp;
		}

		if (dp[i] > mx_dp) {
			mx_dp = dp[i];
			cnt_dp = c[i];
		}

		if (mx <= -a[i] + mx_dp) {
			mx = -a[i] + mx_dp;
			cnt = cnt_dp;
		}
	}
	return {dp.back(), c.back()};
}

void solve() {
	cin >> n >> k;
	a.resize(n);
	cin >> a;
	int cnt = 0, sum = 0;
	bool f = 0;
	for (int i : a) {
		if (i > 0) {
			f = 1;
			sum += i;
		} else if (i < 0) {
			cnt += f;
			f = 0;
		}
	}
	cnt += f;
	if (cnt <= k) {
		cout << sum << "\n";
		return;
	}

	for (int i = 1; i < n; i++)
		a[i] += a[i - 1];

	int l = 0;
	int r = INF;
	while (r - l > 1) {
		int mid = (r + l) / 2;
		if (calc(mid).second > k) {
			l = mid;
		} else {
			r = mid;
		}
	}
	// cerr << calc(0).second <<" " << calc(0).first << endl;
	// return;
	// cerr << r << " " << calc(r).second << endl;
	cout << calc(r).first + k * r;

	// for (int L = -5; L <= 15; L++) {
	// 	cerr << calc(L).second << " ";
	// }
}

int32_t main(int32_t argc, const char * argv[]) {
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(0);
    // insert code here...
    int tt= 1;
    // std::cin >> tt;
    while (tt--) {
        solve();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 23 ms 5456 KB Output is correct
2 Correct 26 ms 5584 KB Output is correct
3 Correct 23 ms 5456 KB Output is correct
4 Correct 28 ms 6100 KB Output is correct
5 Correct 25 ms 5452 KB Output is correct
6 Correct 23 ms 5404 KB Output is correct
7 Correct 23 ms 5460 KB Output is correct
8 Correct 23 ms 5456 KB Output is correct
9 Correct 24 ms 5424 KB Output is correct
10 Correct 23 ms 5460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 111 ms 8564 KB Output is correct
2 Correct 120 ms 8752 KB Output is correct
3 Correct 110 ms 8580 KB Output is correct
4 Correct 18 ms 3676 KB Output is correct
5 Correct 121 ms 10332 KB Output is correct
6 Correct 85 ms 8692 KB Output is correct
7 Correct 16 ms 3932 KB Output is correct
8 Correct 121 ms 10492 KB Output is correct
9 Correct 94 ms 10316 KB Output is correct
10 Correct 16 ms 3668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 153 ms 12580 KB Output is correct
2 Correct 138 ms 10456 KB Output is correct
3 Correct 138 ms 10548 KB Output is correct
4 Correct 99 ms 10440 KB Output is correct
5 Correct 148 ms 10512 KB Output is correct
6 Correct 139 ms 12560 KB Output is correct
7 Correct 126 ms 10660 KB Output is correct
8 Correct 147 ms 10596 KB Output is correct
9 Correct 140 ms 10728 KB Output is correct
10 Correct 152 ms 10580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 452 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 452 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 452 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 528 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 5456 KB Output is correct
2 Correct 26 ms 5584 KB Output is correct
3 Correct 23 ms 5456 KB Output is correct
4 Correct 28 ms 6100 KB Output is correct
5 Correct 25 ms 5452 KB Output is correct
6 Correct 23 ms 5404 KB Output is correct
7 Correct 23 ms 5460 KB Output is correct
8 Correct 23 ms 5456 KB Output is correct
9 Correct 24 ms 5424 KB Output is correct
10 Correct 23 ms 5460 KB Output is correct
11 Correct 111 ms 8564 KB Output is correct
12 Correct 120 ms 8752 KB Output is correct
13 Correct 110 ms 8580 KB Output is correct
14 Correct 18 ms 3676 KB Output is correct
15 Correct 121 ms 10332 KB Output is correct
16 Correct 85 ms 8692 KB Output is correct
17 Correct 16 ms 3932 KB Output is correct
18 Correct 121 ms 10492 KB Output is correct
19 Correct 94 ms 10316 KB Output is correct
20 Correct 16 ms 3668 KB Output is correct
21 Correct 153 ms 12580 KB Output is correct
22 Correct 138 ms 10456 KB Output is correct
23 Correct 138 ms 10548 KB Output is correct
24 Correct 99 ms 10440 KB Output is correct
25 Correct 148 ms 10512 KB Output is correct
26 Correct 139 ms 12560 KB Output is correct
27 Correct 126 ms 10660 KB Output is correct
28 Correct 147 ms 10596 KB Output is correct
29 Correct 140 ms 10728 KB Output is correct
30 Correct 152 ms 10580 KB Output is correct
31 Correct 0 ms 344 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 0 ms 452 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 1 ms 348 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 1 ms 604 KB Output is correct
57 Correct 1 ms 528 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 153 ms 10380 KB Output is correct
62 Correct 26 ms 5716 KB Output is correct
63 Correct 148 ms 10448 KB Output is correct
64 Correct 187 ms 10760 KB Output is correct
65 Correct 176 ms 10548 KB Output is correct
66 Correct 181 ms 10568 KB Output is correct
67 Correct 175 ms 10424 KB Output is correct
68 Correct 147 ms 10576 KB Output is correct
69 Correct 139 ms 10412 KB Output is correct
70 Correct 144 ms 10576 KB Output is correct