Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <algorithm>
- #include <bitset>
- #include <set>
- #include <unordered_set>
- #include <map>
- #include <unordered_map>
- #include <cmath>
- #include <time.h>
- #include <random>
- #include <string>
- #include <cassert>
- #include <vector>
- #include <ostream>
- #include <istream>
- #include <stack>
- #include <deque>
- #include <queue>
- #include <functional>
- #include <chrono>
- #include <stack>
- #include <limits>
- using namespace std;
- #define int long long
- #define pb push_back
- #define all(a) (a).begin(), (a).end()
- #define pii pair<int, int>
- #define ld long double
- istream& operator>> (istream& in, pii& b) {
- in >> b.first >> b.second;
- return in;
- }
- ostream& operator<< (ostream& out, const pii& b) {
- out << "{" << b.first << ", " << b.second << "}";
- return out;
- }
- template<typename T> ostream& operator<< (ostream& out, const vector<T>& a) {
- for (auto k : a) out << k << " ";
- return out;
- }
- template <typename T1, typename T2> inline bool chkmin(T1 &x, const T2 &y) {if (x > y) {x = y; return 1;} return 0;}
- template <typename T1, typename T2> inline bool chkmax(T1 &x, const T2 &y) {if (x < y) {x = y; return 1;} return 0;}
- #ifdef LOCAL
- #define dbg(x) cout << #x << " : " << (x) << endl;
- const long long INF = 1e18;
- // const long long mod = 2600000069;
- // const long long p = 10;
- #else
- #define dbg(x) 57
- const long long INF = 1e18;
- // const long long mod = 2600000069;
- // const long long p = 179;
- #endif
- const ld PI = 4 * atan(1);
- #define time clock() / (double) CLOCKS_PER_SEC
- // #pragma GCC optimize("Ofast,no-stack-protector")
- // #pragma GCC target("sse,sse2,sse3,sse3,sse4")
- // #pragma GCC optimize("unroll-loops")
- // #pragma GCC optimize("fast-math")
- // #pragma GCC target("avx2")
- mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
- const int N = 2010;
- int n, m, k;
- int c[N];
- int a[N][N];
- int up[N][N], down[N][N], lft[N][N];
- int opt[N][N], curans[N][N];
- int t[8 * N], mod[8 * N];
- void build() {
- for (int i = 0; i < 8 * N; i++) {
- t[i] = -INF;
- mod[i] = 0;
- }
- }
- void push(int v) {
- if (mod[v]) {
- t[v] += mod[v];
- mod[2 * v + 1] += mod[v];
- mod[2 * v + 2] += mod[v];
- mod[v] = 0;
- }
- }
- void add(int v, int l, int r, int askl, int askr, int x) {
- push(v);
- if (l >= askr || r <= askl) return;
- if (l >= askl && r <= askr) {
- mod[v] += x;
- push(v);
- return;
- }
- int m = (l + r) / 2;
- add(2 * v + 1, l, m, askl, askr, x);
- add(2 * v + 2, m, r, askl, askr, x);
- t[v] = max(t[2 * v + 1], t[2 * v + 2]);
- }
- void setval(int v, int l, int r, int pos, int val) {
- push(v);
- if (l + 1 == r) {
- t[v] = val;
- return;
- }
- int m = (l + r) / 2;
- if (pos < m) setval(2 * v + 1, l, m, pos, val);
- else setval(2 * v + 2, m, r, pos, val);
- t[v] = max(t[2 * v + 1], t[2 * v + 2]);
- }
- int ans = -INF;
- void D(int v, int l, int r, int pos) {
- push(v);
- if (l + 1 == r) {
- if (l <= pos) cout << t[v] << " ";
- return;
- }
- int m = (l + r) / 2;
- D(2 * v + 1, l, m, pos);
- D(2 * v + 2, m, r, pos);
- }
- signed main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- #ifndef LOCAL
- freopen("submarine.in", "r", stdin);
- freopen("submarine.out", "w", stdout);
- #endif
- cin >> k;
- for (int i = 0; i < k; i++) {
- cin >> c[i];
- }
- cin >> n >> m;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- char ch;
- cin >> ch;
- a[i][j] = c[ch - 'a'];
- }
- }
- for (int i = 1; i < n; i++) {
- for (int j = 0; j < m; j++) {
- up[i][j] = max(0ll, up[i - 1][j] + a[i - 1][j]);
- }
- }
- for (int i = n - 2; i >= 0; i--) {
- for (int j = 0; j < m; j++) {
- down[i][j] = max(0ll, down[i + 1][j] + a[i + 1][j]);
- }
- }
- for (int j = 1; j < m; j++) {
- for (int i = 0; i < n; i++) {
- lft[i][j] = max(0ll, lft[i][j - 1] + a[i][j - 1]);
- }
- }
- vector<pii> s; // {pos, opt}
- // vector<int> t1, t2;
- for (int i = 0; i < n; i++) {
- s.clear();
- build();
- // t1.assign(m, -INF);
- // t2.assign(m, -INF);
- for (int j = 1; j < m; j++) {
- while (!s.empty() && up[i][j] + down[i][j] >= s.back().second) {
- int l, r = s.back().first - 1, was = s.back().second;
- s.pop_back();
- if (s.empty()) {
- l = 0;
- } else {
- l = s.back().first;
- }
- // for (int k = l; k <= r; k++) {
- // t1[k] += up[i][j] + down[i][j] - was;
- // }
- add(0, 0, m, l, r + 1, up[i][j] + down[i][j] - was);
- }
- s.pb({j, up[i][j] + down[i][j]});
- // t1[j - 1] = up[i][j] + down[i][j];
- // t1[j - 1] += lft[i][j - 1] + up[i][j - 1] + a[i][j - 1];
- setval(0, 0, m, j - 1, up[i][j] + down[i][j] + lft[i][j - 1] + up[i][j - 1] + a[i][j - 1]);
- // for (int k = 0; k < j; k++) {
- // t1[k] += a[i][j];
- // }
- add(0, 0, m, 0, j, a[i][j]);
- // for (int k = j - 1; k >= 0; k--) {
- // if (!chkmax(opt[i][k], up[i][j] + down[i][j])) break;
- // }
- // curans[i][j - 1] = lft[i][j - 1] + up[i][j - 1] + a[i][j - 1];
- // for (int k = 0; k < j; k++) {
- // curans[i][k] += a[i][j];
- // chkmax(ans, curans[i][k] + opt[i][k]);
- // }
- chkmax(ans, t[0] + mod[0]);
- // cout << i << " " << j << "\n";
- // for (int k = 0; k < j; k++) {
- // cout << curans[i][k] + opt[i][k] << " ";
- // }
- // cout << "\n";
- // for (int k = 0; k < j; k++) {
- // cout << t1[k] << " ";
- // }
- // cout << "\n--\n";
- // for (int k = 0; k < j; k++) {
- // cout << curans[i][k] << " ";
- // }
- // cout << "\n";
- // for (int k = 0; k < j; k++) {
- // cout << t2[k] << " ";
- // }
- // cout << "\n-----------\n";
- }
- }
- cout << ans << "\n";
- }
- /*
- 2
- -10 1
- 6 11
- aaaaaaaaaaa
- aaabaaaaaaa
- aaabaaaabaa
- abbbbbbbbba
- aaaaaaaabaa
- aaaaaaaaaaa
- -> 13
- 3
- -4 -3 4
- 5 5
- bbabc
- ccaac
- accba
- baccb
- baaaa
- -> 16
- 3
- -2 4 0
- 5 5
- abccb
- cccac
- cbcba
- cccbb
- accba
- -> 24
- 4
- -1 -5 -3 0
- 5 5
- bbabc
- ccaac
- acdba
- baccb
- baaaa
- -> -2
- */
Advertisement
Add Comment
Please, Sign In to add comment