Artifact d0bfb26f428b0c51d21546d6331b6f772d847a8c
#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 <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
class RowGame { public:
long long score(vector <int> board, int k)
{
int N = board.size();
vector<LL> best(N, -1);
best[0] = 0;
LL bestOfBest = 0;
LL dif = -1, loopCnt = 0;
int dir=+1;
for(int ki=0; ki<k; ++ki, dir=-dir)
{
vector<LL> best2(N, -1);
for(int s=0; s<N; ++s)
if( best[s] >= 0 )
{
LL sum = 0;
for(int t=s; 0<=t && t<N; t+=dir) {
sum += board[t];
if( best[s]+sum >= 0 )
best2[t] = max(best2[t], best[s]+sum);
}
}
best.swap(best2);
LL bb2 = max(bestOfBest, *max_element(best.begin(), best.end()));
{
if( bb2-bestOfBest == dif )
++loopCnt;
else {
loopCnt = 0;
dif = bb2-bestOfBest;
}
if( loopCnt >= 2501 )
return bestOfBest + dif*(k-ki);
}
bestOfBest = bb2;
}
return bestOfBest;
}
};
// 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 long long& Expected, const long long& 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(_, RowGame().score(board, k));}
int main(){
CASE(0)
int board_[] = {2,-2,4,3,-10} ;
vector <int> board(board_, board_+sizeof(board_)/sizeof(*board_));
int k = 3;
long long _ = 21LL;
END
CASE(1)
int board_[] = {-6,5};
vector <int> board(board_, board_+sizeof(board_)/sizeof(*board_));
int k = 10;
long long _ = 0LL;
END
CASE(2)
int board_[] = {5,-6};
vector <int> board(board_, board_+sizeof(board_)/sizeof(*board_));
int k = 10;
long long _ = 50LL;
END
CASE(3)
int board_[] = {10,-100,80};
vector <int> board(board_, board_+sizeof(board_)/sizeof(*board_));
int k = 3;
long long _ = 30LL;
END
CASE(4)
int board_[] = {10,-100,80};
vector <int> board(board_, board_+sizeof(board_)/sizeof(*board_));
int k = 4;
long long _ = 90LL;
END
CASE(5)
int board_[] = {-100,1,2,3,4,5,6,7,8,9,10,11,12,13,14};
vector <int> board(board_, board_+sizeof(board_)/sizeof(*board_));
int k = 400000000;
long long _ = 41999999900LL;
END
/*
CASE(6)
int board_[] = ;
vector <int> board(board_, board_+sizeof(board_)/sizeof(*board_));
int k = ;
long long _ = LL;
END
CASE(7)
int board_[] = ;
vector <int> board(board_, board_+sizeof(board_)/sizeof(*board_));
int k = ;
long long _ = LL;
END
*/
}
// END CUT HERE