Okay, this one was tough, segtree with dp? At least it's pretty similar to LIS with dp...
These are just my solutions to some cses.fi problems.