Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <bitset>
- using namespace std;
- vector<long long> elem;
- int n, k;
- bool check_fits(bitset<57> val, int pref, long long check) {
- bitset<57> checkset(check);
- for (int i = 1; i <= pref; i++) {
- if (val[57 - i] && !checkset[57 - i]) {
- return false;
- }
- }
- //cout << "Checfit for " << check << endl;
- return true;
- }
- bool check_for_pref(bitset<57> val, int pref, bool log = false) {
- //if (log)
- // cout << "Checkprefing with " << val.to_string() << " " << pref << endl;
- vector<vector<bool>> dp(n + 1, vector<bool>(k + 1));
- dp[0][0] = true;
- for (int i = 1; i <= n; i++) {
- for (int j = 1; j <= k; j++) {
- long long sum = 0;
- for (int seg = 1; seg <= i && !dp[i][j]; seg++) {
- sum += elem[i - seg];
- dp[i][j] = dp[i][j] | (dp[i - seg][j - 1] && check_fits(val, pref, sum));
- }
- }
- }
- /*if (log) {
- for (int i = 0; i <= n; i++) {
- for (int j = 0; j <= k; j++) {
- cout << dp[i][j] << " ";
- }
- cout << endl;
- }
- }*/
- return dp[n][k];
- }
- bool check(long long checkval) {
- //cout << "Checking " << checkval << endl;
- bitset<57> val(checkval);
- //cout << val.to_string() << endl;
- for (int i = 1; i <= 57; i++) {
- if (val[57 - i] == 0) {
- bitset<57> newval = val;
- newval[57 - i] = true;
- if (check_for_pref(newval, i)) {
- //check_for_pref(newval, i, true);
- return true;
- }
- }
- }
- return false;
- }
- int main() {
- cin >> n >> k;
- elem.resize(n);
- for (int i = 0; i < n; i++) {
- cin >> elem[i];
- }
- long long l = -1, r = ((unsigned long long)1 << (unsigned long long)56);
- while (r > l + 1) {
- long long m = (l + r) / 2;
- if (check(m)) {
- l = m;
- } else {
- r = m;
- }
- }
- cout << r << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement