Codeforces 777E: Hanoi Factory (greedy)

Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings.

There are \(n\) rings in factory's stock. The \(i\)-th ring has inner radius \(a_i\), outer radius \(b_i\) and height \(h_i\). The goal is to select some subset of rings and arrange them such that the following conditions are satisfied:

  • Outer radiuses form a non-increasing sequence, i.e. one can put the \(j\)-th ring on the \(i\)-th ring only if \(b_j ≤ b_i\).
  • Rings should not fall one into the the other. That means one can place ring \(j\) on the ring \(i\) only if \(b_j > a_i\).
  • The total height of all rings used should be maximum possible.

Input

The first line of the input contains a single integer \(n (1 ≤ n ≤ 100 000)\) — the number of rings in factory's stock.

The \(i\)-th of the next \(n\) lines contains three integers \(a_i, b_i\) and \(h_i (1 ≤ a_i, b_i, h_i ≤ 10^9, b_i > a_i)\) — inner radius, outer radius and the height of the \(i\)-th ring respectively.

Output

Print one integer — the maximum height of the tower that can be obtained.

Examples

Input

3
1 5 1
2 6 2
3 7 3

Output

6

Input

4
1 2 1
1 3 3
4 6 2
5 7 1

Output

4

Note

In the first sample, the optimal solution is to take all the rings and put them on each other in order \(3, 2, 1\).

In the second sample, one can put the ring \(3\) on the ring \(4\) and get the tower of height \(3\), or put the ring \(1\) on the ring \(2\) and get the tower of height \(4\).

The meaning of problems

There \ (n-\) a hollow cylinder, the \ (I \) the inner diameter of a cylinder, the outer diameter and height were: \ (a_i, B_i, H_i \) . These cylinders will pile up, requirements: top to bottom, the outer diameter of non-decreasing, and the above outer diameter smaller than the inner diameter below. Q. How high up can heap

Thinking

greedy

Press the outer diameter of the descending order, if the same outer diameter, inner diameter by descending order, if the outer and inner diameters are the same, sorted by descending height. Which can meet if the first \ (i \) one can not take, then the first \ (i + 1 \) one must not take, and have gone to the highest heights

Then with a stack to maintain. Each time a cylinder can be put up in the top of the stack plus, if not put, it will pop the top element, to maintain the total height of the stack within the cylinder, takes a maximum value

Code

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
struct wzy
{
    int a,b,h;
}p[maxn];
bool cmp(wzy u,wzy v)
{
    if(u.b==v.b)
    {
        if(u.a==v.a)
            return u.h>v.h;
        return u.a>v.a;
    }
    return u.b>v.b;
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("/home/wzy/in.txt", "r", stdin);
        freopen("/home/wzy/out.txt", "w", stdout);
        srand((unsigned int)time(NULL));
    #endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>p[i].a>>p[i].b>>p[i].h;
    sort(p+1,p+1+n,cmp);
    ll ans=1LL*p[1].h;
    ll sum=1LL*p[1].h;
    stack<wzy>st;
    st.push(p[1]);
    for(int i=2;i<=n;i++)    
    {
        while(!st.empty()&&(st.top().a>=p[i].b||st.top().b<p[i].b))
        {
            sum-=1LL*st.top().h;
            st.pop();
        }
        sum+=1LL*p[i].h;
        st.push(p[i]);
        ans=max(ans,sum);
    }
    cout<<ans<<endl;
    #ifndef ONLINE_JUDGE
        cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
    #endif
    return 0;
}

Guess you like

Origin www.cnblogs.com/Friends-A/p/11571769.html
Recommended