Artifact Content
Not logged in

Artifact 130fb986bddece4565ac41360b6ea00d4daae32f


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;

template<typename T>
struct DP2
{
	const int N1, N2;
	vector<T> data;
	DP2(int N1, int N2, const T& t = T())
		: N1(N1), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)<(1<<28)); }
	T& operator()(int i1, int i2)
		{ return data[ (i1*N2)+i2 ]; }
	void swap(DP2& rhs)
		{ data.swap(rhs.data); }
};

class SuccessiveSubtraction2 { public:
	vector <int> calc(vector <int> a, vector <int> p, vector <int> v)
	{
		vector<int> answer;
		for(int i=0; i<p.size(); ++i) {
			a[p[i]] = v[i];

			if(a.size() == 1) {
				answer.push_back(a[0]);
				continue;
			}

			answer.push_back(a[0]-a[1]+solve(a));
		}
		return answer;
	}

	int solve(const vector<int>& a)
	{
		DP2<int> dp(a.size(), 5, -0x1fffffff);
		dp(1, 0) = 0;

		for(int i=2; i<a.size(); ++i)
			for(int prev_s=0; prev_s<=4; ++prev_s)
			{
				int s = prev_s;
				dp(i,s) = max(dp(i,s), dp(i-1,prev_s) + a[i]*(s&1 ? +1 : -1));
				if(prev_s<4) {
					s = prev_s + 1;
					dp(i,s) = max(dp(i,s), dp(i-1,prev_s) + a[i]*(s&1 ? +1 : -1));
				}
			}

		int best = 0;
		for(int s=0; s<=4; ++s)
			best = max(best, dp(a.size()-1, s));
		return best;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const vector <int>& Expected, const vector <int>& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, SuccessiveSubtraction2().calc(a, p, v));}
int main(){

CASE(0)
	int a_[] = {1, 2, 3, 4, 5};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int p_[] = {1, 2, 0, 4, 3};
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int v_[] = {3, 9, -10, 5, 1};
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int __[] = {10, 16, 5, 5, 2 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(1)
	int a_[] = {-100, -100, -100, -100, -100};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int p_[] = {0, 1, 2, 3, 4};
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int v_[] = {0, 0, 0, 0, 0};
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int __[] = {400, 300, 200, 100, 0 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(2)
	int a_[] = {83, 0, 25, 21};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int p_[] = {0, 3, 2, 1, 3, 1, 2};
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int v_[] = {10, -90, 33, 52, -100, 0, 45};
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int __[] = {56, 125, 133, 81, 91, 143, 155 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(3)
	int a_[] = {1};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int p_[] = {0, 0, 0, 0};
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int v_[] = {10, -10, 100, -100};
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int __[] = {10, -10, 100, -100 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(4)
	int a_[] = {-11, -4, 28, 38, 21, -29, -45, 11, -58, -39, 92, 35, -56, -6, 29, -2, 61, 10, -29, -63};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int p_[] = {19, 5, 3, 10, 4, 18, 5, 2, 0, 15};
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int v_[] = {-19, 21, 7, -66, 38, -39, -22, 24, -32, 13};
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int __[] = {451, 443, 412, 440, 457, 467, 468, 464, 443, 458 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(5)
int a_[] = {10, 20};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	  int p_[] = {0, 1};
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	  int v_[] = {2, 3};
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	  int __[] = {-18, -1};
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
/*
CASE(6)
	int a_[] = ;
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int p_[] = ;
	  vector <int> p(p_, p_+sizeof(p_)/sizeof(*p_)); 
	int v_[] = ;
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int __[] = ;
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
*/
}
// END CUT HERE