This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Flower_On_Stone
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2005;
const int MAX_SUB4 = 105;
int n, a, b;
long long arr[MAX_N], prefixSum[MAX_N];
int dp[MAX_N];
bool f[MAX_SUB4][MAX_SUB4];
bool sub4(long long pattern, long long cast)
{
long long x = pattern;
for (int i = 0; i < 50; i++)
{
if (!((cast >> i) & 1))
{
x |= (1LL << i);
}
else
{
break;
}
}
memset(f, false, sizeof(f));
f[0][0] = true;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
long long sum = prefixSum[i] - prefixSum[i - j];
if (sum <= x && ((sum & cast) & pattern) == (sum & cast))
{
for (int k = 1; k <= b; k++)
{
f[i][k] |= f[i - j][k - 1];
}
}
}
}
for (int i = a; i <= b; i++)
{
if (f[n][i])
{
return true;
}
}
return false;
}
bool sub5(long long pattern, long long cast)
{
long long x = pattern;
for (int i = 0; i < 50; i++)
{
if (!((cast >> i) & 1))
{
x |= (1LL << i);
}
else
{
break;
}
}
memset(dp, 0x3f, sizeof(int) * (n + 1));
dp[0] = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
long long sum = prefixSum[i] - prefixSum[i - j];
if (sum <= x && ((sum & cast) & pattern) == (sum & cast))
{
dp[i] = min(dp[i], dp[i - j] + 1);
}
}
}
return dp[n] <= b;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> a >> b;
for (int i = 1; i <= n; i++)
{
cin >> arr[i];
prefixSum[i] = prefixSum[i - 1] + arr[i];
}
int maxPos = 0;
for (int i = 1; i < 50; i++)
{
if ((prefixSum[n] >> i) & 1)
{
maxPos = i;
}
}
long long answer = 0, cast = 0;
for (int i = maxPos; i >= 0; i--)
{
cast |= (1LL << i);
if ((a == 1 ? !sub5(answer, cast) : !sub4(answer, cast)))
{
answer |= (1LL << i);
}
}
cout << answer;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |