Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // clang-format off
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <iomanip>
- #include <bitset>
- #include <vector>
- #include <algorithm>
- #include <random>
- #include <map>
- #include <string>
- #include <set>
- #include <deque>
- #include <string>
- #include <cassert>
- #pragma GCC optimize("Ofast")
- using namespace std;
- const int N = 3e3 + 7, MOD = 998244353, A = 26;
- const long double EPS = 1e-10, PI = acos(-1);
- int n, m,q;
- int a[N];
- int cnt_less[N][N];
- int Get_Less(int l, int r, int x) {
- if (l == 0) {
- return cnt_less[x][r];
- }
- return cnt_less[x][r] - cnt_less[x][l - 1];
- }
- signed main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- #else
- std::ios::sync_with_stdio(false);
- cin.tie(0);
- #endif
- cin >> n;
- for (int i = 0; i < n; i++) {
- cin >> a[i];
- }
- for (int i = 0; i < n; i++) {
- if (i == 0){
- for (int j = 0; j <= a[i]; j++) {
- cnt_less[j][i] = 0;
- }
- for (int j = a[i] + 1; j <= n; j++) {
- cnt_less[j][i] = 1;
- }
- }
- else {
- for (int j = 0; j <= n; j++) {
- cnt_less[j][i] = cnt_less[j][i - 1];
- if (a[i] < j) {
- cnt_less[j][i]++;
- }
- }
- }
- }
- long long ANSW = 0;
- for (int i = 0; i < n; i++) {
- for (int j = i + 1; j < n; j++) {
- //cout << i + 1 << " " << j + 1 << " ";
- if (i + 1 == 2 && j + 1 == 4) {
- cout << "";
- }
- int d = j - i;
- int nw_i = Get_Less(0, i, a[i]);
- d = max(d, j - nw_i);
- int nw_j = j + Get_Less(j, n - 1, a[j]);
- d = max(d, nw_j - i);
- d = max(d, abs(a[i] - a[j]));
- ANSW += d;
- //cout << d << "\n";
- }
- }
- cout << ANSW;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement