# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
852941 | Benmath | Election (BOI18_election) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#include<homecoming.h>
#include<bits/stdc++.h>
using namespace std;
long long solve(int N, int K, int *A, int *B){
int n = N;
int k = K;
long long int a[n];
long long int b[n];
long long int dp[n];
long long int dp1[n];
long long int sum1 = 0;
for(int i = 0; i < n; i++){
a[i] = A[i];
b[i] = B[i];
sum1 = sum1 + a[i] - b[i];
//cout << A[i] << " " << B[i] << endl;
dp[i] = 0;
dp1[i] = -1e18;
}
long long int ans = 0;
//cout << sum1 << endl;
ans = max (ans, sum1);
// ako ne uzimamo prvi element
for(int i = 0; i < n; i++){
if (i == 0){
long long int sum = 0;
for (int j = i; j < (i + k); j++){
sum = sum + b[j % n];
}
dp1[i] = a[i] - sum;
dp[i] = max(dp[i], dp1[i]);
}else{
dp[i] = dp[i-1];
long long int sum = 0;
for (int j = i; j < (i + k); j++){
sum = sum + b[j % n];
}
sum = a[i] - sum;
if (i >= k){
dp1[i] = sum + dp[i-k];
}
dp1[i] = max(dp1[i], dp1[i-1] + a[i] - b[(i + k - 1) % n] );
dp[i] = max(dp[i], dp1[i]);
}
}
ans = dp[n-1];
// ako uzimamo prvi element
for (int i = 0; i < n; i++){
dp[i] = -1e18;
dp1[i] = -1e18;
}
for (int i = 0; i < n; i++){
if (i == 0){
long long int sum = 0;
for (int j = i; j < (i + k); j++){
sum = sum + b[j % n];
}
dp1[i] = a[i] - sum;
dp[i] = dp1[i];
}else{
if (i >= k){
dp[i] = dp[i-1];
long long int sum = 0;
for (int j = i; j < (i + k); j++){
sum = sum + b[j % n];
}
sum = a[i] - sum;
dp1[i] = sum + dp[i-k];
dp1[i] = max(dp1[i], dp1[i-1] + a[i] - b[(i + k - 1) % n] );
dp[i] = max(dp[i], dp1[i]);
sum = 0;
for (int j = i; j <= (n-1); j++){
sum = sum + a[j] - b[j];
}
ans = max(ans, sum + dp[i-k]);
}else{
dp[i] = dp[i-1];
dp1[i] = dp1[i-1] + a[i] - b[(i + k - 1) % n];
dp[i] = max(dp[i], dp1[i]);
}
}
}
return ans;
}