#include using namespace std; typedef signed long long ll; #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define FORR2(x,y,arr) for(auto& [x,y]:arr) #define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) //------------------------------------------------------- int N,Q; int A[20202]; template class BIT { public: V bit[1<>N>>Q; FOR(i,N) cin>>A[i]; FOR(i,Q) { int L,R; ZERO(B); ZERO(C); BIT bt; ZERO(bt.bit); cin>>L>>R; L--,R--; ll ret=0; FOR(j,N) { if(jR) { ret+=bt(N)-bt(A[j]); bt.add(A[j],1); if(jR) C[A[j]]++; } } for(j=N+1;j>=1;j--) B[j]+=B[j+1]; for(j=1;j<=N+1;j++) C[j]+=C[j-1]; ll mi=1LL<<60; for(j=1;j<=N;j++) { mi=min(mi,1LL*(R-L+1)*(B[j+1]+C[j-1])); } cout<