#include #define endl '\n' using namespace std; typedef unsigned long long int ULL; const int MAXN = 1e5+5; const int MAXD = 1e5+5; const int MAXK = 555; int N, K, S, W, Z, x, y; int D; int p[MAXD]; vector A(MAXK); vector B(MAXK); string best_sol; ULL best_evalution = -1e18; int numSol = 0; void input(); void solve(); string generateSolution(); ULL evaluteSolution(string); void printSolution(string); mt19937 mt(0); int main () { input(); // cerr << evaluteSolution("UUUUURRRRRRRRRD") << endl << endl; // cin.get(); solve(); printSolution(best_sol); return 0; } void input() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); freopen("cleanUP.in", "r", stdin); freopen("cleanUP.out", "w", stdout); cin >> N >> K >> S >> W >> Z >> x >> y; cerr << N << endl; D = floor(sqrt(2)*(N-1)); for (int i = 0; i <= D; i++) cin >> p[i]; for (int i = 1; i <= K; i++) { cin >> A[i] >> B[i]; } } vector ta, tb; map ,bool> tused; int getPrice(int xi, int yi, char dir) { if (dir == 'U') {xi--;}; if (dir == 'D') {xi++;}; if (dir == 'L') {yi--;}; if (dir == 'R') {yi++;}; if (tused[{xi, yi}]) return 0; int minDist = 2e9; int ind = 0; for (int j = 1; j <= K; j++) { int D = sqrt((ULL)(xi-ta[j])*(xi-ta[j]) + (ULL)(yi-tb[j])*(yi-tb[j])); if (D < minDist) { minDist = D; ind = j; } } return p[minDist]; } string generateSmartSolution() { vector a = A; vector b = B; map ,bool> used; ta = a; tb = b; tused = used; string sol = ""; int xi = x; int yi = y; int step = sqrt(S); if (!step) step = 1; for (int i = 1; i <= S; i++) { // long long minDist = 1e18; used[{xi, yi}] = 1; tused[{xi, yi}] = 1; int oldXi = xi; int oldYi = yi; vector directions = {'L', 'R', 'U', 'D'}; char dir; int remamainingTries = 10; int ccc = 0; for (int k = 0; k < directions.size(); k++) { dir = directions[k]; if (dir == 'U') {xi--;}; if (dir == 'D') {xi++;}; if (dir == 'L') {yi--;}; if (dir == 'R') {yi++;}; ccc++; if (xi < 1 || xi > N || yi < 1 || yi > N) { directions.erase(directions.begin() + k); k--; } xi = oldXi; yi = oldYi; } random_shuffle(directions.begin(), directions.end()); char bestDir = directions[0]; int bestPrice = getPrice(xi, yi, bestDir); for (int j = 1; j < directions.size(); j++) { char tDir = directions[j]; int tPrice = getPrice(xi, yi, tDir); if (tPrice > bestPrice) { bestPrice = tPrice; bestDir = tDir; } } if (bestDir == 'U') {xi--;}; if (bestDir == 'D') {xi++;}; if (bestDir == 'L') {yi--;}; if (bestDir == 'R') {yi++;}; sol += bestDir; if (i % step == 0) { for (int j = 1; j <= K; j++) { a[j] = ((a[j] * W) % N + Z) % N + 1; b[j] = ((b[j] * Z) % N + W) % N + 1; } used.clear(); ta = a; tb = b; tused = used; } } return sol; } string generateSolution() { vector a = A; vector b = B; map ,bool> used; string sol = ""; int xi = x; int yi = y; int step = sqrt(S); if (!step) step = 1; for (int i = 1; i <= S; i++) { long long minDist = 1e18; used[{xi, yi}] = 1; vector directions = {'L', 'R', 'U', 'D'}; char dir; int remamainingTries = 10; while (true) { int oldXi = xi; int oldYi = yi; dir = directions[mt()%directions.size()]; if (dir == 'U') {xi--;}; if (dir == 'D') {xi++;}; if (dir == 'L') {yi--;}; if (dir == 'R') {yi++;}; if (xi < 1 || xi > N || yi < 1 || yi > N) { xi = oldXi; yi = oldYi; for (int j = 0; j < directions.size(); j++) { if (directions[j] == dir) { directions.erase(directions.begin() + j); break; } } continue; } if (used[{xi, yi}]) { if (!remamainingTries) break; remamainingTries--; xi = oldXi; yi = oldYi; continue; } break; } sol += dir; if (i % step == 0) { for (int j = 1; j <= K; j++) { a[j] = ((a[j] * W) % N + Z) % N + 1; b[j] = ((b[j] * Z) % N + W) % N + 1; } used.clear(); } } return sol; } ULL evaluteSolution(string sol) { vector a = A; vector b = B; map , bool> used; ULL ret = 0; int xi = x; int yi = y; int step = sqrt(S); if (!step) step = 1; // cerr << "S: " << S << endl; for (int i = 1; i <= S+1; i++) { ULL minDist = 1e18; int ind = 0; for (int j = 1; j <= K; j++) { ULL D = sqrt((ULL)(xi-a[j])*(xi-a[j]) + (ULL)(yi-b[j])*(yi-b[j])); if (D < minDist) { minDist = D; ind = j; } } if (!used[{xi, yi}]) { ret += p[minDist]; // cerr << i << " Add: " << p[minDist] << endl; } used[{xi, yi}] = 1; if (i != S + 1) { char dir = sol[i-1]; if (dir == 'U') xi--; if (dir == 'D') xi++; if (dir == 'L') yi--; if (dir == 'R') yi++; } if (i % step == 0) { // cerr << i << "Change " << endl; for (int j = 1; j <= K; j++) { a[j] = ((a[j] * W) % N + Z) % N + 1; b[j] = ((b[j] * Z) % N + W) % N + 1; } used.clear(); } } return ret; } void solve() { srand(0); double st = clock(); best_sol = generateSmartSolution(); best_evalution = evaluteSolution(best_sol); double prevTime = (double)(clock() - st) / CLOCKS_PER_SEC; while ((double)clock() / CLOCKS_PER_SEC + prevTime < 4.7) { // cerr << (double)clock() / CLOCKS_PER_SEC << endl; numSol++; string sol; if (numSol % 4 == 0) sol = generateSolution(); else sol = generateSmartSolution(); ULL eval = evaluteSolution(sol); // return; // cerr << "Evaluted: " << eval << endl << endl; if (eval > best_evalution) { best_evalution = eval; best_sol = sol; } } // cerr << "Generated: " << numSol << endl; } void printSolution(string sol) { if (sol.size() < S) exit(4); for (int i = 0; i < S; i++) cout << sol[i]; cout << endl; cerr << best_evalution << endl; } /** 10 2 15 2 1 6 1 8 10 8 1 6 6 4 1 2 6 8 2 7 4 2 3 2 */