Dakar Rally
Description
The Dakar Rally is an annual Dakar Series rally raid type of off-road race, organized by the Amaury Sport Organization. The off-road endurance race consists of a series of routes. In different routes, the competitors cross dunes, mud, camel grass, rocks, erg and so on.
Because of the various circumstances, the mileages consume of the car and the prices of gas vary from each other. Please help the competitors to minimize their payment on gas.
Assume that the car begins with an empty tank and each gas station has infinite gas. The racers need to finish all the routes in order as the test case descripts.

Input

There are multiple test cases. The first line of input contains an integer T (T ≤ 50) indicating the number of test cases. Then T test cases follow.
The first line of each case contains two integers: n – amount of routes in the race; capacity – the capacity of the tank.
The following n lines contain three integers each: mileagei – the mileage of the ith route; consumei – the mileage consume of the car in the ith route , which means the number of gas unit the car consumes in 1 mile; pricei – the price of unit gas in the gas station which locates at the beginning of the ith route.
All integers are positive and no more than 105.

Output

For each test case, print the minimal cost to finish all of the n routes. If it’s impossible, print “Impossible” (without the quotes).

Sample Input
2
2 30
5 6 9
4 7 10
2 30
5 6 9
4 8 10

Sample Output

550
Impossible

#include <cstdio>
#include <iostream>
using namespace std;
struct w
{
   
    long long len,oil,pri;
}s[100010];
int main()
{
   
    long long t,n,m,v,i,j,f,a,r;
    scanf("%lld",&t);
    while(t--)
    {
   
        scanf("%lld%lld",&n,&v);
        f=0;
        for(i=0;i<n;i++)
        {
   
            scanf("%lld%lld%lld",&s[i].len,&s[i].oil,&s[i].pri);
            if(s[i].len*s[i].oil>v)
                f=1;
        }
        if(f)
        {
   
            cout<<"Impossible"<<'\n';
            continue;
        }
        i=0;
        m=0;
        r=0;
        while(i<n)
        {
   
            j=i+1;
            a=s[i].len*s[i].oil;
            while(j<n&&s[j].pri>=s[i].pri&&v>=a+s[j].len*s[j].oil)
            {
   
                a+=s[j].len*s[j].oil;
                j++;
            }
            if(j>=n||s[j].pri<s[i].pri)
            {
   
                if(r>a)
                    r-=a;
                else
                {
   
                    m+=(a-r)*s[i].pri;
                    r=0;
                }
                i=j;
            }
            else
            {
   
                m+=(v-r)*s[i].pri;
                r=v-s[i].len*s[i].oil;
                i++;
            }
        }
        cout<<m<<'\n';
    }
    return 0;
}