1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4 5 int main() 6 { 7 int t; 8 cin >> t; 9 while( t-- ) 10 { 11 int n; 12 cin >> n; 13 int *a = new int[n]; 14 int sum = 0; 15 16 // input data numbers that everyone own the apple 17 for(int i = 0; i < n; i++) 18 { 19 cin >> a[i]; 20 } 21 //cout << "ism " << endl; 22 int current = 0; 23 24 for(int i = 0; i < n; i++) 25 { 26 while( a[i] < 0 ) 27 { 28 if(a[current] <= 0) 29 { 30 current++; 31 } 32 else 33 { 34 if((0-a[i]) <= a[current]) 35 { 36 sum += fabs(current-i)*(0-a[i]); 37 a[current] -= (0-a[i]); 38 a[i] = 0; 39 } 40 else 41 { 42 sum += fabs(current-i)*(a[current]); 43 a[i] += a[current]; 44 a[current] = 0; 45 current++; 46 } 47 48 } 49 } 50 } 51 52 cout << sum << endl; 53 54 delete[] a; 55 } 56 57 return 0; 58 } 59 //end 60 //ism