One day Polycarpus got hold of two non-empty strings *s* and *t*, consisting of lowercase Latin letters. Polycarpus is quite good with strings, so he immediately wondered, how many different pairs of "*x* *y*" are there, such that *x* is a substring of string *s*, *y* is a subsequence of string *t*, and the content of *x* and *y* is the same. Two pairs are considered different, if they contain different substrings of string *s* or different subsequences of string *t*. Read the whole statement to understand the definition of different substrings and subsequences.

The length of string *s* is the number of characters in it. If we denote the length of the string *s* as |*s*|, we can write the string as *s* = *s*<sub class="lower-index">1</sub>*s*<sub class="lower-index">2</sub>... *s*<sub class="lower-index">|*s*|</sub>.

A substring of *s* is a non-empty string *x* = *s*[*a*... *b*] = *s*<sub class="lower-index">*a*</sub>*s*<sub class="lower-index">*a* + 1</sub>... *s*<sub class="lower-index">*b*</sub> (1 ≤ *a* ≤ *b* ≤ |*s*|). For example, "code" and "force" are substrings or "codeforces", while "coders" is not. Two substrings *s*[*a*... *b*] and *s*[*c*... *d*] are considered to be different if *a* ≠ *c* or *b* ≠ *d*. For example, if *s*="codeforces", *s*[2...2] and *s*[6...6] are different, though their content is the same.

A subsequence of *s* is a non-empty string *y* = *s*[*p*<sub class="lower-index">1</sub>*p*<sub class="lower-index">2</sub>... *p*<sub class="lower-index">|*y*|</sub>] = *s*<sub class="lower-index">*p*<sub class="lower-index">1</sub></sub>*s*<sub class="lower-index">*p*<sub class="lower-index">2</sub></sub>... *s*<sub class="lower-index">*p*<sub class="lower-index">|*y*|</sub></sub> (1 ≤ *p*<sub class="lower-index">1</sub> &lt; *p*<sub class="lower-index">2</sub> &lt; ... &lt; *p*<sub class="lower-index">|*y*|</sub> ≤ |*s*|). For example, "coders" is a subsequence of "codeforces". Two subsequences *u* = *s*[*p*<sub class="lower-index">1</sub>*p*<sub class="lower-index">2</sub>... *p*<sub class="lower-index">|*u*|</sub>] and *v* = *s*[*q*<sub class="lower-index">1</sub>*q*<sub class="lower-index">2</sub>... *q*<sub class="lower-index">|*v*|</sub>] are considered different if the sequences *p* and *q* are different.
The input consists of two lines. The first of them contains *s* (1 ≤ |*s*| ≤ 5000), and the second one contains *t* (1 ≤ |*t*| ≤ 5000). Both strings consist of lowercase Latin letters.
aa
aa
5
codeforces
forceofcode
60
coderscontest
codeforces
39
a
b
0
ab
bbbba
5
abbbccbba
abcabc
33
zxzxzxzxzxzxzx
d
0
pfdempfohomnpgbeegikfmflnalbbajpnpgeacaicoehopgnabnklheepnlnflohjegcciflmfjhachnhekckfjgoffhkblncidn
idlhklpclcghngeggpjdgefccihndpoikojdjnbkdfjgaanoolfmnifnmbpeeghpicehjdiipnlfnjpglidpnnnjfmjfhbcogojkcfflmmfcgajbjbfaikhmjofbnjnaolbcdkelcieeodbfjcfiblhhmeelmpcmmcdhjcnnfklgedjjbaljndjonanlojclboeelkab
26774278
sbypoaavsbqxfiqvpbjyimhzotlxuramhdamvyobsgaehwhtfdvgvlxpophtvzrmvyxwyzeauyatzitsqvlabufbcefaivwzoccfvhrdbzlmzoofczqzbxoqioctzzxqksuorhnldrfavlhyfyobvnqsyegsbvlusxchixbddzbwwnvuulcarguxvnvzkdqcjxxdetll
lbawrainjjdgkdmwkqzxlwxhzjyrxbuwjhnvjlnmgfyrxdynsanvibfhngsioisbyldaplituqhebeicpsyerpiiqpjtnxvuotjv
8095
ababbabbab
ababb
75
bbabb
bababbbbab
222
ababababab
ababb
74
b
ab
1
ab
a
1
xzzxxxzxzzzxzzzxxzzxzzxzxzxxzxxzxxzxzzxxzxxzxxxzxzxzxxzzxxxxzxzzzxxxzxzxxxzzxxzxxzxxzzxxzxxzxzxzzzxzzzzxzxxzzxzxxzxxzzxzxzx
zzx
291
zxx
xzzxxxxzzzxxxzzxxxzxxxzzxxzxxxzzxzxxzxxzxxzxzxxzxxxxzzxxzzxzzxxzzxxzxzxzxxzxzxzxxxzxxzzxxzxxxxzzxxxzxxxzxzxzzxzzxxxxzzxxzxz
46917
Q.1 Non overlapping trees
vector<int> tree;
int maxXor(int root){
int max_val = 0;
for(int i=0; i<tree.size(); i++){
if(tree[i] != root && (tree[i]^root) > max_val){
max_val = tree[i]^root;
}
}
return max_val;
}
int findMaxXorSubtrees(int root, vector<int> A) {
// Write your code here
tree = A;
int max_val = 0;
for(int i=0; i<tree.size(); i++){
int curr_xor = maxXor(tree[i]);
if(curr_xor > max_val){
max_val = curr_xor;
}
}
return max_val;
}

Q.2 Candy question
According to my analysis, the min-sum for each root will be when we will consider the root itself. For the max sum, we can use DFS. We will take each vertex as a root and do the same kind of operation for each of them. We will store the answers and then we will return the maximum value among them. I have tried to write the logic with the code itself again in the form comments. 

CODE: 

#include<bits/stdc++.h>
using namespace std;

//in each dfs, we go on each way and take the max path. so we will test each route, and return the max value returned

int dfs(int x, vector<bool>&visited, vector<int>&B, vector<int>adj[]){
    visited[x]=true;
    int maxSum=0;
    //here we are checking all the unvisited nodes, connected to x and checking for the max value. 
    for(auto i:adj[x]){
        if(visited[i]==false)
            maxSum=max(maxSum,dfs(i,visited,B,adj));
    }
    visited[x]=false;
    //returning the maxSum +B[x] since we also need to add the weight of current node we are at
    return maxSum+B[x];
}

int main(){
    int A;cin>>A; //for number of vertex
    vector<int>B(A);
    for(int i=0;i<A;i++)
        cin>>B[i];
    vector<int>adj[A];
    for(int i=0;i<A-1;i++){
        int x,y;
        cin>>x>>y;
        adj[x-1].push_back(y-1);
        adj[y-1].push_back(x-1);
    }
    int maxValOfDiff=0;//declaring the maximum difference value to be 0
    //calling the function to get max sum and min sum at each vertex
    for(int i=0;i<A;i++){
        //calling for each edge
        int maxSum=0;
        //declaring a visited array to keep track of which nodes are visited
        vector<bool>visited(A,false);
        //marking the current node visited
        visited[i]=true;
        for(auto x:adj[i]){
            maxSum=max(maxSum,dfs(x,visited,B,adj)+B[i]);
        }
        int minSum=B[i];
        //calculating the maxValOfDiff each time 
        maxValOfDiff = max(maxValOfDiff,maxSum-minSum);
    }
    cout<<maxValOfDiff<<endl;
    return 0;
}

Q.3 
## taking input from txt file, you can replace the code with input()

with open('input.txt') as f:
    lines = f.readlines()

from heapq import *

class solve():
    def __init__(self,a,b,c):
        self.a = a
        self.b = b
        self.c = c
        self.ans = [float("inf")]*a

        self.m = {i:[] for i in range(a)}
        self.cost = {}

        for a,b,c,t in c:
            self.m[a].append([b,c,t])
            self.m[b].append([a,c,t])
    #dijkstra algo
    def dk(self,s):
        pq = [[0,s]]
        visited = set()
        self.ans[s] = min(self.ans[s],self.b[s])

        while pq:
            w,cur = heappop(pq)
            if cur in visited:
                continue
            visited.add(cur)
            for node,c,t in self.m[cur]:
                cost = (w+c)*(1+t) + self.b[node]
                print(cost)
                self.ans[cur] = min(cost,self.ans[cur])
                heappush(pq,[w+c,node])


    def solution(self):
        for i in range(self.a):
            # applying for each node, not very optimized
            self.dk(i)
        return self.ans

for _ in range(1):
    a = int(lines[0])
    b = list(map(int,lines[1].split()))
    c = []
    for i in range(a-1):
        row = list(map(int,lines[2].split()))
        c.append(row)
    obj = solve(a,b,c)
    print(obj.solution())


struct edge
{
    int node, tax, factor;
};

struct auxStruct
{
    int node, cost;

    friend bool operator<(auxStruct a, auxStruct b) {return a.cost<b.cost;}
    //I did not test the code, if it doesn't work, maybe change to>
};

std::vector<int> solve(int A, std::vector<int>& B, std::vector<std::vector<int> >&C)
{
    std::vector<edge> road[100005];
    std::vector<int> minimum(A);
    std::priority_queue<auxStruct> q;
    int i, x;
    bool checked[100005];

    for(i=0;i<C.size();++i)
    {
        std::vector<int> x=C[i];
        road[x[0]].node=x[1];
        road[x[0]].tax=x[2];
        road[x[0]].factor=x[3];
        road[x[1]].node=x[0];
        road[x[1]].tax=x[2];
        road[x[1]].factor=x[3];
    }

    for(i=0;i<A;++i)
    {
        q.push(i);
        minimum[i]=B[i];
        checked[i]=0;
    }

    while(!q.empty())
    {
        x=q.top();
        if(!checked[x])
        {
            checked[x]=1;
            for(i=0;i<road[x].size();++i)
            {
                m=minimum[x]+(1+road[x][i].factor)*road[x][i].tax;
                if(m<minimum[road[x][i].node])
                {
                    minimum[road[x][i].node]=m;
                    q.push({road[x][i].node, m});
                }
            }
        }
    }

    return minimum;
}