#include #include #include using namespace std; #include // a // a + k + 10 // a + 10 // a + k + 11 int main() { cin.tie(nullptr)->sync_with_stdio(false); int N; cin >> N; vector A(N); for (auto &x : A) cin >> x; vector idx(N); for (int i = 0; i < N; ++i) idx.at(i) = i; stable_sort(idx.begin(), idx.end(), [&](int i, int j) { return A.at(i) < A.at(j); }); atcoder::fenwick_tree fw(N); long long ret = 0; for (const int i : idx) { const int sum = fw.sum(0, i + 1); for (int j = i + 1; j < N; ++j) { if (A.at(j) == A.at(i) + 1) ret += sum; } vector cntr(2001); int nbad = 0; for (int i1 = i - 1; i1 >= 0; --i1) { if (A.at(i1) + 10 == A.at(i)) { fw.add(i1, 1); fw.add(i, -1); ret -= nbad; } if (A.at(i1) > A.at(i)) nbad += cntr.at(A.at(i1) + 1); cntr.at(A.at(i1))++; } } cout << ret << '\n'; }