#include #include #include #include #include namespace { size_t Merge(std::vector& a, size_t i, size_t chunk_size, std::vector& tmp) { std::vector::iterator a1 = a.begin() + i; size_t l1 = chunk_size; std::vector::iterator a2 = a1 + l1; size_t l2 = (a.size() > i + chunk_size * 2) ? chunk_size : a.size() - i - chunk_size; tmp.clear(); size_t res = 0; for (size_t i1 = 0, i2 = 0; i1 != l1 || i2 != l2; ) { if (i1 == l1) { tmp.push_back(a2[i2++]); } else if (i2 == l2) { tmp.push_back(a1[i1++]); } else if (a1[i1] < a2[i2]) { tmp.push_back(a1[i1++]); } else if (a2[i2] < a1[i1]) { tmp.push_back(a2[i2++]); res += l1 - i1; } else { std::cerr << "Oops" << std::endl; } } for (size_t di = 0; di < tmp.size(); ++di) a[i + di] = tmp[di]; return res; } size_t CountCross(std::vector& a) { size_t res = 0; std::vector tmp(a.size()); for (size_t chunk_size = 1; chunk_size < a.size(); chunk_size *= 2) for (size_t i = 0; i + chunk_size < a.size(); i += chunk_size * 2) res += Merge(a, i, chunk_size, tmp); return res; } } int N; int x; int main(int argc, char **argv) { scanf("%d",&N); std::vector M; for(int i = 0; i < N; i++){ scanf("%d",&x); M.push_back(x); } std::cout << CountCross(M) << std::endl; }