#include using namespace std; typedef long long ll; template void check_min(T &a, const T &b){ a = (a < b) ? a : b; } template void check_max(T &a, const T &b){ a = (a > b) ? a : b; } #define all(x) (x).begin(), (x).end() const int N = 1e5 + 3; const int K = 50 + 3; const int INF = 1e9; int n, k, s, w, z, x, y; int p[2 * N]; int start_x, start_y; int a[N], b[N]; int start_a[N], start_b[N]; tuple adj[4]{{-1, 0, 'U'}, {0, -1, 'L'}, {1, 0, 'D'}, {0, 1, 'R'}}; clock_t timer = clock(); bool time_left(){ return (((float)clock() - (float)timer) / (float)CLOCKS_PER_SEC) <= 4; } int dist(int x1, int y1, int x2, int y2){ return sqrt((ll)(x1 - x2) * (x1 - x2) + (ll)(y1 - y2) * (y1 - y2)); } int get_d(int x, int y){ int d = 2 * n; for(int i = 0; i < k; ++i) check_min(d, dist(x, y, a[i], b[i])); return d; } mt19937 mt(1); pair runTarget(int target){ for(int i = 0; i < k; ++i){ a[i] = start_a[i]; b[i] = start_b[i]; } x = start_x; y = start_y; int change = sqrt((double)s); set> vis; string letters = ""; ll score = p[get_d(x, y)]; for(int i = 1; i <= s; ++i){ vis.insert({x, y}); if(i % change == 0){ for(int j = 0; j < k; ++j){ a[j] = ((ll)a[j] * w + z) % n + 1; b[j] = ((ll)b[j] * z + w) % n + 1; } vis.clear(); } vector, char>> options; char next_letter; for(auto [dx, dy, dir]: adj){ auto [to_x, to_y] = pair{x + dx, y + dy}; if(to_x < 1 || to_y < 1 || n < to_x || n < to_y) continue; if(vis.count({to_x, to_y})){ int d = get_d(to_x, to_y); options.push_back(pair{array{-INF - abs(d - target), -1, to_x, to_y}, dir}); continue; } int d = get_d(to_x, to_y); options.push_back(pair{array{-abs(d - target), p[d], to_x, to_y}, dir}); } sort(all(options)); auto best = options[options.size() - 1]; x = best.first[2]; y = best.first[3]; letters += best.second; if(best.first[1] != -1) score += best.first[1]; } return {score, letters}; } double randomDouble(){ const int C = 1e9; return (mt() % C) / (double)C; } pair runRandom(double chance){ for(int i = 0; i < k; ++i){ a[i] = start_a[i]; b[i] = start_b[i]; } x = start_x; y = start_y; int change = sqrt((double)s); set> vis; string letters = ""; ll score = p[get_d(x, y)]; for(int i = 1; i <= s; ++i){ vis.insert({x, y}); if(i % change == 0){ for(int j = 0; j < k; ++j){ a[j] = ((ll)a[j] * w + z) % n + 1; b[j] = ((ll)b[j] * z + w) % n + 1; } vis.clear(); } vector, char>> options; char next_letter; for(auto [dx, dy, dir]: adj){ auto [to_x, to_y] = pair{x + dx, y + dy}; if(to_x < 1 || to_y < 1 || n < to_x || n < to_y) continue; if(vis.count({to_x, to_y})){ options.push_back(pair{array{-1, to_x, to_y}, dir}); continue; } int d = get_d(to_x, to_y); options.push_back(pair{array{p[d], to_x, to_y}, dir}); } sort(all(options), greater, char>>()); int real_options = 0; for(; real_options < options.size(); ++real_options){ if(options[real_options].first[0] == -1){ break; } } if(!real_options) real_options = 1; auto best = options[mt() % real_options]; if(randomDouble() <= chance) best = options[0]; x = best.first[1]; y = best.first[2]; letters += best.second; if(best.first[0] != -1) score += best.first[0]; } return {score, letters}; } int main(){ ios::sync_with_stdio(false); cin.tie(NULL); freopen("cleanUP.in", "r", stdin); freopen("cleanUP.out", "w", stdout); cin >> n >> k >> s >> w >> z >> x >> y; start_x = x; start_y = y; int cnt = (n - 1) * (double)sqrt((double)2.0); for(int i = 0; i <= cnt; ++i) cin >> p[i]; for(int i = 0; i < k; ++i){ cin >> a[i] >> b[i]; start_a[i] = a[i]; start_b[i] = b[i]; } vector perm(cnt + 1); iota(all(perm), 0); sort(all(perm), [&](int l, int r){ return p[l] > p[r]; }); pair best{-1, ""}; clock_t timer2 = clock(); check_max(best, runRandom(1.0)); long double time_for_1 = ((long double)clock() - (long double)timer2) / (long double)CLOCKS_PER_SEC; for(int i = 0; i <= cnt && time_left(); ++i){ check_max(best, runTarget(perm[i])); } int cnt_left = (4.8 - (long double)clock() / CLOCKS_PER_SEC) / time_for_1; // cout << cnt_left << " " << time_for_1 << endl; while(time_left()){ for(int i = 1; i <= cnt_left && time_left(); ++i){ check_max(best, runRandom(1.0 - i / (double)cnt_left)); } } // cout << "score: " << best.first << "\n"; cout << best.second << "\n"; }