Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * author: compounding
- * created: 2024-12-14 22:59:34
- **/
- #include <bits/stdc++.h>
- using namespace std;
- mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
- #define NeedForSpeed \
- ios_base::sync_with_stdio(false); \
- cin.tie(NULL); \
- cout.tie(NULL);
- #define int long long
- #define all(x) (x).begin(), (x).end()
- typedef vector<int> vi;
- typedef vector<bool> vb;
- typedef vector<vi> vvi;
- typedef vector<pair<int, int>> vpi;
- #define f first
- #define s second
- #define yes cout << "YES" << endl
- #define no cout << "NO" << endl
- #define endl "\n"
- const int mod = 1000000007;
- int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
- void solve()
- {
- int h, w, X;
- cin >> h >> w >> X;
- int x, y;
- cin >> x >> y;
- int grid[h + 1][w + 1];
- for (int i = 1; i <= h; i++)
- {
- for (int j = 1; j <= w; j++)
- {
- cin >> grid[i][j];
- }
- }
- vvi visited(h + 1, vi(w + 1, 0));
- int sum = grid[x][y];
- int dx[] = {0, 0, 1, -1};
- int dy[] = {1, -1, 0, 0};
- priority_queue<pair<int, pair<int, int>>> pq; // maximum priority queue, implementation using -ve sign
- pq.push({0, {x, y}});
- visited[x][y] = 1;
- while (!pq.empty())
- {
- auto [size, now] = pq.top();
- auto [nowx, nowy] = now;
- pq.pop();
- int cur_size = -size;
- if (cur_size >= (sum + X - 1) / X)
- {
- break;
- }
- sum += cur_size; // absorb the maximum value
- for (int i = 0; i < 4; i++)
- {
- int newx = nowx + dx[i];
- int newy = nowy + dy[i];
- if (newx >= 1 && newx <= h && newy >= 1 && newy <= w && !visited[newx][newy])
- {
- if (!visited[newx][newy])
- {
- visited[newx][newy] = 1;
- pq.push({-grid[newx][newy], {newx, newy}});
- }
- }
- }
- }
- cout << sum << endl;
- }
- signed main()
- {
- NeedForSpeed;
- int t = 1;
- // cin >> t;
- while (t--)
- {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment