Subsequence Weighting

Problem Statement :

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. 

You are given a sequence A in which every element is a pair of integers  i.e  A = [(a1, w1), (a2, w2),..., (aN, wN)].

For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : 

We call it increasing if for every i (1 <= i < M ) , bi < bi+1.
Weight(B) = v1 + v2 + ... + vM.
Given a sequence, output the maximum weight formed by an increasing subsequence.

The first line of input contains a single integer T. T test-cases follow. The first line of each test-case contains an integer N. The next line contains a1, a2 ,... , aN separated by a single space. The next line contains w1, w2, ..., wN separated by a single space.

For each test-case output a single integer: The maximum weight of increasing subsequences of the given sequence.

1 <= T <= 5
1 <= N <= 150000
1 <= ai <= 109, where i ∈ [1..N]
1 <= wi <= 109, where i ∈ [1..N]

Sample Input:

1 2 3 4  
10 20 30 40  
1 2 3 4 1 2 3 4  
10 20 30 40 15 15 15 50
Sample Output:


Solution :


                            Solution in C :

In  C++ :

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>

using namespace std;

#define GI ({int new_input;scanf("%d",&new_input);new_input;})
typedef unsigned long long ll;

ll Tree[800000];
void updateTree(int b, int e, int p, ll  val, int idx=1) {
	if(p < b || p > e) return ;
	if(p == b && p == e){ 
		Tree[idx] = max(Tree[idx],val);
		return ;
	int mid = (b+e)/2;
	int lt = (idx<<1);
	int rt = ((idx<<1)+1);
	updateTree(b, mid, p, val, lt);
	updateTree(mid+1, e, p, val, rt);
	Tree[idx] = max(Tree[lt], Tree[rt]);
	return ;
ll query(int b,int e,int start,int end,int node){
	if(e<start || b>end)return 0;
	if(b<=start && e>=end)return Tree[node];
	int mid=(start+end)>>1;
	return max(query(b,e,start,mid,node*2),query(b,e,mid+1,end,node*2+1));	
ll input[200000];
ll w[200000];
int main() {
    int t=GI;
		memset(Tree,0,sizeof Tree);
		int n=GI;
		for(int i=0;i<n;i++){
		for(int i=0;i<n;i++){
		int in=1;
		set<ll>::iterator it;
		ll ans=0;
		for(int i=0;i<n;i++){
			int mapped=m[input[i]];
				ll get=query(1,mapped-1,1,in,1);
    return  0;

In Java :

import java.util.Map.Entry;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.SortedMap;
import java.util.TreeMap;

public class Solution {

	public static void main(String[] args) {

		Scanner sc = new Scanner(;
		int  nProb = sc.nextInt();
		for(int k = 1; k <= nProb; ++k) {
			int n = sc.nextInt();
			int[] a = new int[n];
			int[] w = new int[n];
			for(int i = 0; i < n; ++i)
				a[i] = sc.nextInt();
			for(int i = 0; i < n; ++i)
				w[i] = sc.nextInt();
			long bestW = solve(a, w);

	private static long solve(int[] a, int[] w) {
		int n = a.length;
		long best = 0;
		TreeMap<Integer, Long> map = new TreeMap<Integer, Long>();
		for(int k = 0; k < n; ++k) {
			Entry<Integer, Long> e = map.lowerEntry(a[k]);
			long b = (e == null ? 0 : e.getValue()) + w[k];
			SortedMap<Integer, Long> tail = map.tailMap(a[k]);
			List<Integer> del = new ArrayList<Integer>();
			for(Entry<Integer, Long> x : tail.entrySet()) {
				if(x.getValue().longValue() > b)
			for(Integer i : del) {
				map.put(a[k], b);
			if(best < b)
				best = b;
		return best;

In C :

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct treap {
    int x, p;
    long long y;
    struct treap *l, *r;
}* Treap;

Treap td = NULL;

Treap newTreap(int x, long long y) {
    Treap t;
    if (td) {
        t = td;
        td = td->r;
        t = (Treap) malloc(sizeof(struct treap));
    t->x = x;
    t->y = y;
    t->p = rand();
    t->l = t->r = NULL;
    return t;

void dump(Treap t) {
    if (t) {
        t->r = td;
        td = t;

Treap merge(Treap l, Treap r) {
    if (!l)
        return r;
    if (!r)
        return l;
    if (l->p > r->p) {
        l->r = merge(l->r, r);
        return l;
    r->l = merge(l, r->l);
    return r;

void split(Treap t, Treap *l, Treap *r, long long v, int d) {
    if (!t)
        *l = *r = NULL;
    else if (d ? (t->x < v) : (t->y <= v)) {
        split(t->r, &t->r, r, v, d);
        *l = t;
    else {
        split(t->l, l, &t->l, v, d);
        *r = t;

Treap rightmost(Treap t) {
    if (t) {
        while (t->r)
            t = t->r;
    return t;

Treap leftmost(Treap t) {
    if (t) {
        while (t->l)
            t = t->l;
    return t;

long long solve() {
    int n, i;
    long long v;
    scanf("%d", &n);
    int a[n], w[n];
    for (i = -1; ++i < n; scanf("%d", a + i));
    for (i = -1; ++i < n; scanf("%d", w + i));
    Treap r = newTreap(0, 0), l, m;
    for (i = -1; ++i < n;) {
        split(r, &l, &r, a[i], 1);
        m = rightmost(l);
        v = w[i] + m->y;
        split(r, &m, &r, v, 0);
        if (m) {
            m = newTreap(a[i], v);
        else {
            m = leftmost(r);
            if (!m || m->x > a[i])
                m = newTreap(a[i], v);
                m = NULL;
        l = merge(l, m);
        r = merge(l, r);
    v = rightmost(r)->y;
    return v;

int main() {
    int t;
    scanf("%d", &t);
    while (t--)
        printf("%lld\n", solve());
    return 0;

In Python3 :

import os
import sys
import bisect
# Complete the solve function below.
def solve(a, w):
    b = [[0,0],[10000000000,10000000000]]
    for i in range(len(a)):
        g = [a[i],w[i]]
        ind = b.index(g)
        if b[ind+1][0] != b[ind][0] and b[ind-1][0] != b[ind][0]:
            for j in range(ind+1,len(b)):
                if b[j][1] >b[ind][1]:
            b = b[:ind+1] + b[j:]
        elif b[ind+1][0] == b[ind][0]:
            if b[ind+1][1]>=b[ind][1]:
                for j in range(ind+1,len(b)):
                    if b[j][1]>b[ind][1]:
                b = b[: ind+1] + b[j: ]
        elif b[ind-1][0] ==b[ind][0]:
            b[ind][1] += b[ind-2][1]
            if b[ind-1][1] >= b[ind][1]:
                for j in range(ind+1,len(b)):
                     if b[j][1]>b[ind][1]:
                b = b[: ind+1] + b[j: ]
    return b[-2][1]
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')
    t = int(input())
    for t_itr in range(t):
        n = int(input())
        a = list(map(int, input().rstrip().split()))
        w = list(map(int, input().rstrip().split()))
        result = solve(a, w)
        fptr.write(str(result) + '\n')

View More Similar Problems

Sparse Arrays

There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results. Example: strings=['ab', 'ab', 'abc'] queries=['ab', 'abc', 'bc'] There are instances of 'ab', 1 of 'abc' and 0 of 'bc'. For each query, add an element to the return array, results=[2,1,0]. Fun

View Solution →

Array Manipulation

Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array. Example: n=10 queries=[[1,5,3], [4,8,7], [6,9,1]] Queries are interpreted as follows: a b k 1 5 3 4 8 7 6 9 1 Add the valu

View Solution →

Print the Elements of a Linked List

This is an to practice traversing a linked list. Given a pointer to the head node of a linked list, print each node's data element, one per line. If the head pointer is null (indicating the list is empty), there is nothing to print. Function Description: Complete the printLinkedList function in the editor below. printLinkedList has the following parameter(s): 1.SinglyLinkedListNode

View Solution →

Insert a Node at the Tail of a Linked List

You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty. Input Format: You have to complete the SinglyLink

View Solution →

Insert a Node at the head of a Linked List

Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty. Function Description: Complete the function insertNodeAtHead in the editor below

View Solution →

Insert a node at a specific position in a linked list

Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e

View Solution →