Submission #254243

# Submission time Handle Problem Language Result Execution time Memory
254243 2020-07-29T15:19:51 Z amoo_safar Homecoming (BOI18_homecoming) C++14
0 / 100
98 ms 35576 KB
// Null
#include <bits/stdc++.h>


#ifndef safar
#include "homecoming.h"
#endif

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'

using namespace std;

typedef long long ll;

const int NN = 2e6 + 10;
const ll Inf = 1e18;

ll dp[2][NN];

ll solve(int n, int k, int *A, int *B){
	ll ans = 0;
	ll S2 = 0;
	for(int i = 0; i < n; i++) S2 += A[i];
	for(int i = 0; i < n; i++) S2 -= B[i];

	if(n == k) return max(0ll, S2);

	fill(dp[0], dp[0] + NN, -Inf);
	fill(dp[1], dp[1] + NN, -Inf);
	
	dp[0][0] = 0;
	ll Sm = B[0];
	for(int i = 1; i < n; i++){
		Sm += B[i];
		dp[0][i] = max(dp[0][i - 1], dp[1][i - 1]);
		if(i >= k){
			Sm -= B[i - k];
			dp[1][i] = max(dp[1][i - 1] - B[i] + A[i - k + 1], dp[0][i-k] + A[i - k + 1] - Sm);
		}
	}
	ans = max(ans, dp[0][n - 1] + dp[1][n - 1]);

	fill(dp[0], dp[0] + NN, -Inf);
	fill(dp[1], dp[1] + NN, -Inf);
	dp[1][0] = 0;

	Sm = 0;
	for(int i = 0; i < k; i++) Sm += A[(n - i) % n];

	for(int i = 1; i < n; i++){
		Sm += A[i];
		Sm -= A[(i - k + n) % n];

		dp[1][i] = max(dp[0][i - 1], dp[1][i - 1]);
		
		dp[0][i] = max(dp[0][i - 1] + B[i] - A[i], dp[1][i - 1] + B[i] - Sm);
	}
	S2 = 0;
	for(int i = 0; i < n; i++) S2 += ((ll)A[i]) - B[i];

	ans = max(ans, S2 + max(dp[0][n - 1], dp[1][n - 1]));
	ans = max(ans, S2);
	if(k == n){
		assert(ans == max(0ll, S2));
	//	assert(false);
	}
	return ans;
}

#ifdef safar
int Ai[3] = {40, 80, 0}, Bi[3] = {140, 0, 20};
int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cout << solve(3, 2, Ai, Bi) << '\n';
	return 0;
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 20 ms 31616 KB Output is correct
2 Correct 20 ms 31616 KB Output is correct
3 Incorrect 98 ms 31736 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 31616 KB Output is correct
2 Correct 20 ms 31616 KB Output is correct
3 Incorrect 98 ms 31736 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 50 ms 35576 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 31616 KB Output is correct
2 Correct 20 ms 31616 KB Output is correct
3 Incorrect 98 ms 31736 KB Output isn't correct
4 Halted 0 ms 0 KB -