由于num[i]比较大,所以要hash一下;然后就是树状数组的事情了;
/*****************************************
Author :Crazy_AC(JamesQi)
Time :2015
File Name :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
#define MEM(a,b) memset(a,b,sizeof a)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
inline int Readint(){
char c = getchar();
while(!isdigit(c)) c = getchar();
int x = 0;
while(isdigit(c)){
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
map<int,int> mp;
int n;
int res[500010],bit[500010],num[500010];
inline int lowbit(int x){
return x & (-x);
}
int get(int x){
int ret = 0;
while(x >= 1){
ret += bit[x];
x -= lowbit(x);
}
return ret;
}
void add(int x,int v){
while(x <= n){
bit[x] += v;
x += lowbit(x);
}
}
void solve(){
LL sum = 0LL;
memset(bit,0,sizeof bit);
for (int i = 1;i <= n;i++){
sum += get(n) - get(mp[num[i]]);
add(mp[num[i]],1);
}
printf("%lld\n",sum);
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(scanf("%d",&n) != EOF && n){
mp.clear();
for (int i = 1;i <= n;i++){
scanf("%d",&num[i]);
res[i] = num[i];
}
sort(res + 1,res + 1 + n);
for (int i = 1;i <= n;i++){
mp[res[i]] = i;
}
solve();
}
return 0;
}