Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
- // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx")
- // #pragma comment(linker, "/stack:200000000"]
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <algorithm>
- #include <unordered_set>
- #include <unordered_map>
- #include <set>
- #include <map>
- #include <queue>
- #include <deque>
- #include <bitset>
- #include <stack>
- #include <random>
- #include <fstream>
- #include <sstream>
- #include <chrono>
- #define fi first
- #define se second
- #define pb push_back
- #define ll long long
- #define ld long double
- #define hm unordered_map
- #define pii pair<int, int>
- #define sz(a) (int)a.size()
- #define all(a) a.begin(), a.end()
- #define cinv(v) for (auto& x: v) cin >> x
- #define fr(i, n) for (int i = 0; i < n; ++i)
- #define fl(i, l, n) for (int i = l; i < n; ++i)
- #define int ll
- 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;}
- using namespace std;
- #ifdef LOCAL
- #define dbg(x) cerr << #x << " : " << x << '\n'
- const int maxn = 20;
- #else
- #define dbg(x)
- const int maxn = 2e5 + 20;
- #endif
- //tg: @galebickosikasa
- ostream& operator << (ostream& out, vector<int>& v) {
- for (auto& x: v) out << x << ' ';
- return out;
- }
- ostream& operator << (ostream& out, pii& v) {
- out << v.fi << ", " << v.se;
- return out;
- }
- istream& operator >> (istream& in, pii& a) {
- in >> a.fi >> a.se;
- return in;
- }
- const ll inf = (ll) 2e9;
- const ld pi = asin (1) * 2;
- const ld eps = 1e-8;
- const ll mod = (ll)1e9 + 7;
- const ll ns = 97;
- mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
- int check1 (int st, int fn, vector<int>& a) {
- int i = sz (a) - 1, j = 0;
- int ans = 0;
- fr (k, inf) {
- ans += abs (a[i] - a[j]);
- --i;
- if (i == j) {
- break;
- }
- ans += abs (a[i] - a[j]);
- ++j;
- if (i == j) {
- break;
- }
- }
- return ans;
- }
- int check2 (int st, int fn, vector<int>& a) {
- int i = 0, j = sz (a) - 1;
- int ans = 0;
- fr (k, inf) {
- ans += abs (a[i] - a[j]);
- ++i;
- if (i == j) {
- break;
- }
- ans += abs (a[i] - a[j]);
- --j;
- if (i == j) {
- break;
- }
- }
- return ans;
- }
- pair<vector<vector<int>>, int> check1 (int n) {
- vector<vector<int>> res (n);
- int last = -1;
- int i = n - 3, j = 0;
- fr (k, inf) {
- res[i].pb (j);
- res[j].pb (i);
- --i;
- if (i == j) {
- last = i;
- break;
- }
- res[i].pb (j);
- res[j].pb (i);
- ++j;
- if (i == j) {
- last = j;
- break;
- }
- }
- return {res, last};
- }
- pair<vector<vector<int>>, int> check2 (int n) {
- vector<vector<int>> res (n);
- int last = -1;
- int i = 0, j = n - 3;
- fr (k, inf) {
- res[i].pb (j);
- res[j].pb (i);
- ++i;
- if (i == j) {
- last = i;
- break;
- }
- res[i].pb (j);
- res[j].pb (i);
- --j;
- if (i == j) {
- last = j;
- break;
- }
- }
- return {res, last};
- }
- signed main () {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n;
- cin >> n;
- vector<int> goo (n);
- cinv (goo);
- if (n == 1) {
- cout << 0 << '\n';
- exit (0);
- }
- if (n == 2) {
- cout << 1 + abs (goo[0] - goo[1]) << '\n';
- exit (0);
- }
- auto ptt1 = check1 (n), ptt2 = check2 (n);
- vector<int> a;
- int ans = 0;
- dbg (ptt1.se);
- dbg (ptt2.se);
- fr (st, n - 1) {
- a.clear ();
- fr (i, n) {
- if (i == st || i == st + 1) continue;
- a.pb (i);
- }
- int c1 = check1 (st, st + 1, a);
- int c2 = check2 (st, st + 1, a);
- ans = max (ans, c1 + abs (st - a.back ()) + abs (st + 1 - a[ptt1.se]) + goo[st + 1] - goo[st]);
- ans = max (ans, c1 + abs (st + 1 - a.back ()) + abs (st - a[ptt1.se]) + goo[st] - goo[st + 1]);
- ans = max (ans, c2 + abs (st - a.front ()) + abs (st + 1 - a[ptt2.se]) + goo[st + 1] - goo[st]);
- ans = max (ans, c2 + abs (st + 1 - a.front ()) + abs (st - a[ptt2.se]) + goo[st] - goo[st + 1]);
- dbg (st);
- dbg (ans);
- dbg (c1);
- dbg (c2);
- fl (fn, st + 2, n) {
- int i = fn - 2;
- for (auto& x: ptt1.fi[i]) {
- c1 -= abs (a[i] - a[x]);
- }
- for (auto& x: ptt2.fi[i]) {
- c2 -= abs (a[i] - a[x]);
- }
- --a[i];
- for (auto& x: ptt1.fi[i]) {
- c1 += abs (a[i] - a[x]);
- }
- for (auto& x: ptt2.fi[i]) {
- c2 += abs (a[i] - a[x]);
- }
- ans = max (ans, c1 + abs (st - a.back ()) + abs (fn - a[ptt1.se]) + goo[fn] - goo[st]);
- ans = max (ans, c1 + abs (fn - a.back ()) + abs (st - a[ptt1.se]) + goo[st] - goo[fn]);
- ans = max (ans, c2 + abs (st - a.front ()) + abs (fn - a[ptt2.se]) + goo[fn] - goo[st]);
- ans = max (ans, c2 + abs (fn - a.front ()) + abs (st - a[ptt2.se]) + goo[st] - goo[fn]);
- dbg (st);
- dbg (fn);
- dbg (ans);
- dbg (c1);
- dbg (c2);
- dbg (a);
- dbg (abs (fn - a[ptt1.se]));
- dbg (goo[fn] - goo[st]);
- dbg (abs (st - a.back ()));
- }
- }
- cout << ans << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement