Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
591 B
3
Indexable
Never
#include <iostream>
using namespace std;

int price[31];
int ans;


int main()
{
	
	int tc, T;
	freopen("input.txt", "r", stdin);

	cin >> T;
	for(tc = 0; tc < T; tc++)
	{
	ans=0;
	int n;
	cin>>n;
	for(int i=0;i<n;i++) cin>>price[i];
	
	for(int i=n-1; i>0;i--) {
		int count=0;
		for(int j=i-1; j>=0; j--) {
			if(price[j]<price[i])count++;
			if(price[j]>=price[i]||j==0) {
				ans+=count*price[i];
				while(count>0) {
					ans=ans-price[i-count];
					count--;
				}
				i=j+1;
				break;
			}
		}
	}
	cout<<"#"<<tc+1<<" "<<ans;
	}
	
	return 0;
}