import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {
    static BigInteger[] dp = new BigInteger[10000];

    static BigInteger fib(int n) {
        n -= 1;
        if (n <= 1) return BigInteger.ONE;
        if (dp[n] != null) return dp[n];
        dp[0] = BigInteger.ONE;
        dp[1] = BigInteger.ONE;
        dp[n] = dp[n-1].add(dp[n-2]);
        return dp[n];
    }
    static long solve(int n) {
        if(n<=2) return n*2;
        long [] dp = new long[n];
        dp[0] = 2;
        dp[1] = 4;
        dp[2] = 7;
        int numberOfWhite = 1;
        for (int i = 3; i < n; i++) {
            dp[i] = (dp[i-1]-numberOfWhite)*2 + 1;
            numberOfWhite*=2;
        }
        return dp[n-1];
    }
    private static int convert(int first, int second) {
        int k = 1;
        int equation = second-first+k*26;
        while(equation<97) {
            k++;
            equation = second-first+k*26;
        }
        return equation;
    }
    private static boolean check(int n) {
        return n>=97 && n<=122;
    }
    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        InputReader.OutputWriter out = new InputReader.OutputWriter(outputStream);

        int n = in.nextInt();
        int k = in.nextInt();
        int [] a = new int[n];
        for(int i=0;i<n;i++) {
            a[i]=in.nextInt();
        }
        int [] dp = new int[n];
        dp[0]=0;
        Arrays.fill(dp,1,dp.length,Integer.MAX_VALUE);
        for(int i=0;i<n;i++) {
            for(int j=i+1;j<=i+k;j++) {
                if(j<n) {
                    dp[j] = dp[i]+Math.abs(a[i]-a[j]);
                }
            }
        }
        System.out.println(dp[n-1]);
        out.flush();
    }
    static int currentDrawBack(int a,int b,int c, int d) {
        int max = Math.max(a,b);
        int min = Math.min(c,d);
        return min-max+1;
    }
    static int lowerBound1(Integer [] a,int target) {
        int lo = 0;
        int hi = a.length - 1;
        while(lo<=hi) {
            int med = hi-(hi-lo)/2;
            if(target<=a[med]) hi = med - 1;
            else lo = med + 1;
        }
        return lo;
    }
    static int lowerBound(int [] a,int target) {
        int lo = 0;
        int hi = a.length - 1;
        while (lo<=hi) {
            int med = hi-(hi-lo)/2;
            if(target<=a[med]) {
                hi = med - 1;
            }
            else {
                lo = med + 1;
            }
        }
        return lo;
    }

    private static void reverse(char [] c, int k, int index) {
        for(int i=index;i<(2*index+k)/2;i++) {
            System.out.println(i + " " + (k-index - 1));
            char temp = c[i];
            c[i] = c[k-i-1];
            c[k-i-1] = temp;
        }

    }
    static boolean isVowel(char c) {
        return c=='a' || c=='e' || c=='i' || c=='o' || c=='u';
    }
    private static String whoWin(int [] a) {
        TreeMap<Integer,Integer> map = new TreeMap<>(Comparator.reverseOrder());
        for(int n:a) {
            map.put(n,map.getOrDefault(n,0)+1);
        }
        for(Map.Entry<Integer,Integer> m:map.entrySet()) {
            if(m.getValue()%2==1) return "Conan";
        }
        return "Agasa";
    }

    private static int [] prefixSum(int [] a) {
        int [] prefix = new int[a.length+1];
        for(int i=1;i < prefix.length;i++) {
            prefix[i] = prefix[i-1] + a[i-1];
        }
        return prefix;
    }
    private static int [] charFrequency(String a) {
        int [] freq = new int[26];
        char [] cc = a.toCharArray();
        for(char c : cc) {
            freq[c-'a']++;
        }
        return freq;
    }
}

class InputReader extends BufferedReader {
    StringTokenizer tokenizer;

    public InputReader(InputStream inputStream) {
        super(new InputStreamReader(inputStream), 32768);
    }

    public InputReader(String filename) {
        super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)));
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(readLine());
            } catch (IOException e) {
                throw new RuntimeException();
            }
        }
        return tokenizer.nextToken();
    }

    public Integer nextInt() {
        return Integer.valueOf(next());
    }
    public Long nextLong() {return  Long.valueOf(next());}

    static class OutputWriter extends PrintWriter {
        public OutputWriter(OutputStream outputStream) {
            super(outputStream);
        }

        public OutputWriter(Writer writer) {
            super(writer);
        }

        public OutputWriter(String filename) throws FileNotFoundException {
            super(filename);
        }

        public void close() {
            super.close();
        }
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		//		new Main().dPA();
		new Main().dPA2();

	}

	private void dPA() {
		Scanner scanner = null;
		int numFrog = 0;

		try {
			scanner = new Scanner(System.in);
			numFrog = scanner.nextInt();
			int[] heightArray = new int[numFrog];

			for (int i = 0; i < numFrog; i++) {
				heightArray[i] = scanner.nextInt();
			}

			int[] koufukuArray = new int[numFrog];
			for (int i = 0; i < heightArray.length; i++) {
				if (i != 0 && i != 1) {
					int val1 = Math.abs(heightArray[i] - heightArray[i - 1]);
					int val2 = Math.abs(heightArray[i] - heightArray[i - 2]);
					koufukuArray[i] = Math.min(koufukuArray[i - 1] + val1, koufukuArray[i - 2] + val2);
				} else if (i == 0) {
					koufukuArray[i] = 0;
				} else if (i == 1) {
					koufukuArray[i] = koufukuArray[i - 1] + Math.abs(heightArray[i] - heightArray[i - 1]);
				}

			}
			System.out.println(koufukuArray[koufukuArray.length - 1]);

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

	private void dPA2() {
		Scanner scanner = null;
		int numFrog = 0;
		int jump = 2;

		try {
			scanner = new Scanner(System.in);
			numFrog = scanner.nextInt();
			int[] heightArray = new int[numFrog];

			for (int i = 0; i < numFrog; i++) {
				heightArray[i] = scanner.nextInt();
			}

			int[] cost = new int[numFrog];
			Arrays.fill(cost, Integer.MAX_VALUE);

			cost[0] = 0;

			for (int i = 0; i < heightArray.length; i++) {

				for (int j = 1; j <= jump; j++) {
					if (i + j < heightArray.length) {
						cost[i + j] = Math.min(cost[i + j], Math.abs(heightArray[i + j] - heightArray[i]) + cost[i]);
					}
				}

			}
			System.out.println(cost[cost.length - 1]);
		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}
}

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score : 100 points

Problem Statement

There are N stones, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), the height of Stone i is h_i.

There is a frog who is initially on Stone 1.
He will repeat the following action some number of times to reach Stone N:

If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, i + K. Here, a cost of |h_i - h_j| is incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq K \leq 100

1 \leq h_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N K
h_1 h_2 \ldots h_N

Output

Print the minimum possible total cost incurred.

Sample Input 1

5 3
10 30 40 50 20

Sample Output 1

30

If we follow the path 1 → 2 → 5, the total cost incurred would be |10 - 30| + |30 - 20| = 30.

Sample Input 2

3 1
10 20 10

Sample Output 2

20

If we follow the path 1 → 2 → 3, the total cost incurred would be |10 - 20| + |20 - 10| = 20.

Sample Input 3

2 100
10 10

Sample Output 3

0

If we follow the path 1 → 2, the total cost incurred would be |10 - 10| = 0.

Sample Input 4

10 4
40 10 20 70 80 10 20 70 80 60

Sample Output 4

40

If we follow the path 1 → 4 → 8 → 10, the total cost incurred would be |40 - 70| + |70 - 70| + |70 - 60| = 40.

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Azhan Khan
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastScanner in = new FastScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        BFrog2 solver = new BFrog2();
        solver.solve(1, in, out);
        out.close();
    }

    static class BFrog2 {
        public void solve(int testNumber, FastScanner in, PrintWriter out) {
            int n = in.nextInt();
            int k = in.nextInt();
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = in.nextInt();
            }

            int[] dp = new int[n];
            //dp[0] = 0;
            dp[1] = Math.abs(arr[1] - arr[0]);
            for (int i = 1; i < n; i++) {
                dp[i] = dp[i - 1] + Math.abs(arr[i] - arr[i - 1]);
                for (int j = Math.max(0, i - k); j < i; j++) {
                    dp[i] = Math.min(dp[i], dp[j] + Math.abs(arr[i] - arr[j]));
                }
                //dp[i] = Math.min(dp[i - 2] + value2, dp[i - 1] + value1);
            }

            out.print(dp[n - 1]);
        }

    }

    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(InputStream inputStream) {
            br = new BufferedReader(new InputStreamReader(inputStream));
        }

        public String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.util.*;
import java.io.*;

public class Main {

  private static void solve() {
    int n = nextInt();
    int K = nextInt();
    int[] h = new int[n];

    for (int i = 0; i < n; i++) {
      h[i] = nextInt();
    }

    int[] dp = new int[n];

    Arrays.fill(dp, Integer.MAX_VALUE);
    dp[0] = 0;
    for (int i = 0; i < n; i++) {
      for (int k = 1; k <= K; k++) {
        if (i + k < n) {
          dp[i + k] = Math.min(dp[i + k], dp[i] + Math.abs(h[i + k] - h[i]));
        }
      }
    }

    System.out.println(dp[n - 1]);
  }

  private static void run() {
    br = new BufferedReader(new InputStreamReader(System.in));
    out = new PrintWriter(System.out);

    solve();

    out.close();
  }

  private static StringTokenizer st;
  private static BufferedReader br;
  private static PrintWriter out;

  private static String next() {
    while (st == null || !st.hasMoreElements()) {
      String s;
      try {
        s = br.readLine();
      } catch (IOException e) {
        return null;
      }
      st = new StringTokenizer(s);
    }
    return st.nextToken();
  }

  private static int nextInt() {
    return Integer.parseInt(next());
  }

  private static long nextLong() {
    return Long.parseLong(next());
  }

  public static void main(String[] args) {
    run();
  }
}


import java.util.*;
import java.io.*;

public class Main {
	static BufferedReader br;
	static StringTokenizer tokenizer;

	public static void main(String[] args) throws Exception {
		br = new BufferedReader(new InputStreamReader(System.in));
		int n = nextInt();
		int m = nextInt();
		int[] arr = new int[n];
		int[] dp = new int[n];
		for(int i = 0; i < n; i++) {
			arr[i] = nextInt();
			dp[i] = Integer.MAX_VALUE;
		}
		dp[0] = 0;
		for(int i = 0; i < n - 1; i++) {
			int max = Math.min(i + m, n - 1) - i;
			for(int k = 1; k <= max; k++) {
			dp[i + k] = Math.min(dp[i + k], Math.abs(arr[i] - arr[i + k]) + dp[i]);
			}
		}
		System.out.println(dp[n - 1]);
	}

	public static String next() throws IOException {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			String line = br.readLine();
			if (line == null)
				throw new IOException();
			tokenizer = new StringTokenizer(line);
		}
		return tokenizer.nextToken();
	}

	public static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}
}


package main;

import java.io.PrintWriter;
import java.util.*;
public class Main {

	static int c;
	static int []w;
	static int []v;
	static long [][]dp;
	static int n;

	public static long KnapSack(int idx,int remw) {
		if(remw<0)
			return -(int) 1e9;
		if(idx==n)
			return 0;
		if(dp[idx][remw]!=-1)
			return  dp[idx][remw];
		long take=v[idx]+KnapSack(idx+1,remw-w[idx]);
		long leave=KnapSack(idx+1,remw);
		return dp[idx][remw]=Math.max(take, leave);
	}

	public static void main(String[] args) {
    Scanner sc=new Scanner (System.in);
    PrintWriter pw=new PrintWriter(System.out);
    n=sc.nextInt();
    c=sc.nextInt();
    w=new int[n];
    v=new int[n];
    dp=new long[n][c+1];
    for(long[] x:dp)
        Arrays.fill(x, -1);
    for(int i=0;i<n;i++) {
    	w[i]=sc.nextInt();
    	v[i]=sc.nextInt();
    }
    pw.println(KnapSack(0,c));
    pw.flush();
	}

}

Score : 100 points

Problem Statement

There are N items, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.

Taro has decided to choose some of the N items and carry them home in a knapsack.
The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.

Find the maximum possible sum of the values of items that Taro takes home.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq W \leq 10^5

1 \leq w_i \leq W

1 \leq v_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible sum of the values of items that Taro takes home.

Sample Input 1

3 8
3 30
4 50
5 60

Sample Output 1

90

Items 1 and 3 should be taken.
Then, the sum of the weights is 3 + 5 = 8, and the sum of the values is 30 + 60 = 90.

Sample Input 2

5 5
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

Sample Output 2

5000000000

The answer may not fit into a 32-bit integer type.

Sample Input 3

6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3

17

Items 2, 4 and 5 should be taken.
Then, the sum of the weights is 5 + 6 + 3 = 14, and the sum of the values is 6 + 6 + 5 = 17.


import java.io.*;
import java.util.*;
public class Main{
  	/*public int minRec(int a[],int dp[],int i,int j)
    {
      if(i==n && j==n)
        	return dp[n][n];
      if(
        return Math.min( Math.abs(a[]
    }*/
	public static void main(String args[]) throws IOException{
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
	  int W = sc.nextInt();
      int w[]=new int[n];
      int v[] = new int[n];

      for(int i=0; i<n; i++){
         w[i] = sc.nextInt();
         v[i] = sc.nextInt();
      }

      int dp[][] =  new int[n][3];

      for(int i=0; i<n; i++)
      Arrays.fill(dp[i], Integer.MIN_VALUE);

      System.out.println(maxSum(w, v, W, 0));

    }

  public static int maxSum(int wt[], int v[], int W, int pos){

  	if(pos >= wt.length)
      return 0;

  //  if(prevSelected!=-1 && dp[pos][prevSelected] != Integer.MIN_VALUE)
    //  return dp[pos][prevSelected];

    int sum = 0;
    if(wt[pos] > W)
      return maxSum(wt, v, W, pos+1);

    return Math.max(v[pos]+ maxSum(wt, v, W-wt[pos], pos+1), maxSum(wt, v, W, pos+1));

  }
}


//package cf;
import java.io.*;
import java.util.*;
public class Main {
    static int p=1000000007;
    public static void main(String[] args) throws Exception{
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 512);
        FastReader sc=new FastReader();
        int n = sc.nextInt();
        int W = sc.nextInt();
        int w[]=new int[n];
        int v[]=new int[n];
        for(int i=0;i<n;i++)
        {
            w[i]=sc.nextInt();
            v[i]=sc.nextInt();
        }

        StringBuilder sb=new StringBuilder();
        long dp[][]=new long[n+1][W+1];
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=W;j++)
            {
                if(j-w[i-1]>=0)
                {
                    dp[i][j]=Math.max(dp[i-1][j],v[i-1]+dp[i-1][j-w[i-1]]);
                 //   System.out.println(i+"  "+j+" "+dp[i][j]);
                }
                else
                {
                    dp[i][j]=Math.max(dp[i][j],dp[i-1][j]);
                }
            }
        }
        sb.append(dp[n][W]);
        out.write(sb.toString());
        out.flush();
    }

static class pair
{
    long a,d;
    StringBuilder sb;

    public pair(long a, long d, StringBuilder sb) {
        this.a = a;
        this.d = d;
        this.sb = sb;
    }
}

    ///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
    static class FastReader {

        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

}


import java.util.*;
import java.io.*;

class Main {

    static class Reader {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader() {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1) {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');

            if (c == '.') {
                while ((c = read()) >= '0' && c <= '9') {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException {
            if (din == null)
                return;
            din.close();
        }
    }

    static final Reader in = new Reader();
    static int N, W; // ..maximum capacity of knapsack
    static int[] w; // ..weight array
    static int[] v; // .. value or price array
    static HashSet<Integer> items_set = new HashSet<>();

    static long[][] dp;

    public static void main(String[] args) throws Exception {
        N = in.nextInt();
        W = in.nextInt();
        w = new int[N + 1];
        v = new int[N + 1];

        dp = new long[N + 1][W + 1];

        for (int i = 1; i <= N; i++) {
            w[i] = in.nextInt();
            v[i] = in.nextInt();
            items_set.add(i);
            Arrays.fill(dp[i], -1);
        }

        System.out.println(knapSack(W, N));
    }

    static long knapSack(int c, int i) {
        // base condition
        if (i < 0)
            return 0;

        //..if value already exists directly return
        if (dp[i][c] != -1)
            return dp[i][c];

        if (w[i] <= c) {
            //..ignore ith element and consider rest of i-1 elements
            long temp1 = knapSack(c, i - 1);
            //..consider ith element so reduce capacity
            long temp2 = v[i] + knapSack(c - w[i], i - 1);

            //..store it
            dp[i][c] = Math.max(temp1,temp2);

            return dp[i][c];

        } else { // ..weight of ith element is more than c
            dp[i][c] = knapSack(c, i - 1);
            return dp[i][c];
        }
    }

    static long k(int W, int i) {
        // base condition
        if (i < 0)
            return 0;

        if (dp[i][W] != -1)
            return dp[i][W];

        if (w[i] <= W) {
            dp[i][W] = Math.max(v[i] + knapSack(W - w[i],i - 1), knapSack(W,i - 1));
            return dp[i][W];

        } else {
            dp[i][W] = knapSack(W,i - 1);
            return dp[i][W];
        }
    }

}


import java.util.*;
import java.io.*;
class Main {
    public static long solve(int[] weight, int[] value, int w, int i, long[][] dp){
        if(i >= weight.length || w <= 0){
            return 0;
        }
        if(dp[i][w] != -1){
            return dp[i][w];
        }
        long ans = Long.MIN_VALUE;
        ans = Math.max(ans, solve(weight, value, w, i+1, dp));
        if(w-weight[i] >= 0){
            ans = Math.max(ans, value[i] + solve(weight, value, w-weight[i], i+1, dp));
        }
        dp[i][w] = ans;
        return ans;
    }
    public static void main(String args[] ) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      	String[] s = br.readLine().trim().split(" ");
  		int n = Integer.parseInt(s[0]);
        int w = Integer.parseInt(s[1]);
      	int[] weight = new int[n];
        int[] value = new int[n];
        for(int i = 0; i < n; i++){
        	String[] str = br.readLine().trim().split(" ");
          	weight[i] = Integer.parseInt(str[0]);
          	value[i] = Integer.parseInt(str[1]);
        }

        long[][] dp = new long[n+1][w+1];
        for(int j = 0; j < dp.length; j++){
          for(int k = 0; k < dp[0].length; k++){
            dp[j][k] = -1;
          }
        }
        System.out.println(solve(weight, value, w, 0, dp));
    }
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt(), W = sc.nextInt(), w[] = new int[N], v[] = new int[N];
		long dp[][] = new long[N][W + 1];
		for (int i = 0; i < N; i++) {
			w[i] = sc.nextInt();
			v[i] = sc.nextInt();
		}
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < W + 1; j++) {
				long temp = (i > 0 && j >= w[i]) ? dp[i - 1][j - w[i]] : 0;
				long temp2 = (i > 0) ? dp[i - 1][j] : 0;
				if (j >= w[i])
					dp[i][j] = Long.max(temp + v[i], temp2);
				else
					dp[i][j] = temp2;
			}
		}
		sc.close();
		System.out.println(dp[N - 1][W]);
	}
}


import java.util.*;
import java.io.*;
class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        int C = Integer.parseInt(st.nextToken());
        int[] wt = new int[N];
        int[] val = new int[N];

        for (int i=0; i<N; i++){
            st = new StringTokenizer(br.readLine());
            wt[i] = Integer.parseInt(st.nextToken());
            val[i] = Integer.parseInt(st.nextToken());
        }

        long[][] dp = new long[N+1][C+1];

        for (int i=1; i<=N; i++){
            for (int j = 1; j <= C; j++) {
                dp[i][j] = Math.max(dp[i][j], dp[i-1][j]);
                if(j-wt[i-1]>=0)
                    dp[i][j] = Math.max(dp[i][j], dp[i-1][j-wt[i-1]]+val[i-1]);
//                System.out.print(dp[i][j]+" ");
            }
//            System.out.println();
        }

        System.out.println(dp[N][C]);

    }
}


import java.io.*;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
	// write your code here
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
        StringTokenizer st;
        //knapsack probk
        st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int [] val = new int[N+1];
        int [] wt = new int[N+1];
        int W = Integer.parseInt(st.nextToken());
        long [][] K  = new long[N+1][W+1];
        for (int i=0;i<N;i++){
            st = new StringTokenizer(br.readLine());
            wt[i] = Integer.parseInt(st.nextToken());
            val[i] = Integer.parseInt(st.nextToken());
        }
        for (int i=0;i<=N;i++){
            for (int w =0;w<=W;w++){
             if (i==0|w==0)
                 K[i][w] = 0;
             else if (wt[i-1]<=w){
                 K[i][w] = Math.max(val[i-1]+K[i-1][w-wt[i-1]],K[i-1][w]);
             }else
                 K[i][w] = K[i-1][w];
            }
        }
        pw.println(K[N][W]);
        pw.flush();
    }
}


//package src;
import java.io.*;
//import javafx.util.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
	public static void main (String[] args)  {
//		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//		int t = Integer.parseInt(br.readLine());
//		while(t-->0)
//		{
//		  int totItems = Integer.parseInt(br.readLine()); //no.of items.
//		  int n = Integer.parseInt(br.readLine()); //capacity of knapsack.
//		  int val[] = new int[totItems]; //values of items.
//		  int wt[] = new int[totItems]; //weights of items.
//
//
//		  for(int i=0;i<totItems;i++){
//            String s1[]= br.readLine().split(" ");
//		    val[i] = Integer.parseInt(s1[i]);
//            wt[i] = Integer.parseInt(s1[i]);
//          }

		   // String s2[]= br.readLine().split(" ");
		//  for(int i=0;i<;i++)
		  //  wt[i] = Integer.parseInt(s2[i]);
		Scanner sc =new Scanner(System.in);
		int totItems=sc.nextInt();
		int n=sc.nextInt();
		int val[]=new int [totItems];
		int wt[]=new int [totItems];
		for(int i=0;i<totItems;i++) {
		 wt[i]=sc.nextInt();
		val[i]=sc.nextInt();
		}
		    findMaxProfit(totItems,n,val,wt);

	}

	static void findMaxProfit(int totItems,int n,int val[],int wt[])
	{
	    int dp[][] = new int[n+1][totItems+1];
	   //IMP:-Top X-axis - > Capacity from 0....to n.    Draw graph to visualise.
	    //    Y-axis - > Items from 0...to totItems.

	    //First row and first column is filled with 0's.

	    for(int i=1;i<=n;i++)
	    {
	        for(int j=1;j<=totItems;j++)
	        {
	           //If the weight of the item is less than or equal to the capacity.
	            if(wt[j-1]<=i) {
	   /*IMP:-  Take the max of(val of cur_item + (DP answer -> [cur_capacity
	            -weight of cur_item][above item/cell])
	                                AND
	            (DP answer -> dp[same capacity][above item/cell] */

	            dp[i][j] = Math.max(val[j-1]+dp[i-wt[j-1]][j-1] , dp[i][j-1]);   }

	            else
	            dp[i][j] = dp[i][j-1];
	        }
	    }
	    //Gives the max value obtained for the capacity n considering all the items.
	    System.out.println(dp[n][totItems]) ;
	}
}


import java.io.*;
import java.util.*;

class Main {

    public static long solve(int[] weights, int[] val, int W, int n, long[][] memo){
        if(n <= 0 || W <= 0)
            return 0;
        if(memo[n][W] > -1){
            return memo[n][W];
        }
        long ans = Long.MIN_VALUE;
        if(W - weights[n-1] >= 0)
            ans = Math.max(ans,val[n-1] + solve( weights,val,W-weights[n-1],n-1,memo));

        ans = Math.max(ans, solve( weights,val,W,n-1,memo));

        return memo[n][W] = ans;

    }

    public static void main(String[] args) throws Exception {
        BufferedReader br  =  new BufferedReader(new InputStreamReader(System.in));
        String[] nw = br.readLine().trim().split(" ");
        int n = Integer.parseInt(nw[0]);
        int W = Integer.parseInt(nw[1]);
        int weights[] = new int[n];
        int val[] = new int[n];

        for(int i  = 0; i < n; i++){
            String[] input = br.readLine().trim().split(" ");

            weights[i] = Integer.parseInt(input[0]);
            val[i] = Integer.parseInt(input[1]);
        }

        long[][] memo = new long[n+1][W+1];
        for(int i = 0; i < memo.length; i++)
            Arrays.fill(memo[i],-1);
        System.out.println(solve(weights,val,W,n,memo));

    }
}


import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
        Scanner reader = new Scanner(System.in);
        int n = reader.nextInt();
        int W = reader.nextInt();
        int[] w = new int[n];
        int[] v = new int[n];
        for (int i = 0; i < n; ++i) {
            w[i] = reader.nextInt();
            v[i] = reader.nextInt();
        }
        System.out.println(solve(w, v, W));
    }

    private static long solve(int[] w, int[] v, int W) {
        boolean[] taken = new boolean[w.length];
        long[][] dp = new long[w.length][W + 1];
        for (int i = 0; i < w.length; ++i)
            Arrays.fill(dp[i], -1);
        long max = calcMax(-1, w, v, W, w.length, taken, dp);
//        System.out.println(Arrays.toString(dp));
        return max;

    }

    private static long calcMax(int ind, int[] w, int[] v, int W, int n, boolean[] taken, long[][] dp) {
        if (ind >= 0 && dp[ind][W] >= 0)
            return dp[ind][W];
        long max = 0;
        for (int i = 0; i < taken.length; ++i) {
            if (!taken[i] && (W - w[i] >= 0)) {
                taken[i] = true;
                max = Math.max(calcMax(i, w, v, W - w[i], n - 1, taken, dp), max);
                dp[i][W] = max;
                taken[i] = false;
            }
        }
        long res = max;
        if (ind >= 0) {
            res += v[ind];
//            dp[ind] = res;
        }
        return res;
//        return res;
    }
}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {
    static int N, W;
    static int[][] item;
    static long[][] dp;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
//        Reader sc = Reader();
        N = sc.nextInt();
        W = sc.nextInt();
        PrintWriter pw = new PrintWriter(System.out);

        item = new int[N+1][2];

        for(int i=1; i<=N; ++i) {
            item[i][0] = sc.nextInt();
            item[i][1] = sc.nextInt();
        }

        pw.println(Knapsack_1(N, W, item));
        pw.close();
    }

    public static void d0(long[][] dp) {
        System.out.print("    ");

        for(int j=0; j<dp[0].length; ++j){
            System.out.printf("%3d ", j);
        }
        System.out.println();

        for(int i=0; i<dp.length; ++i){
            System.out.printf("%3d|", i);
            for(int j=0; j<dp[0].length; ++j){
                System.out.printf("%3d ", dp[i][j]);
            }
            System.out.println();
        }
    }

    public static int myMax(int a, int b) {
        if(a>b)
            return a;

        return b;
    }

    public static long Knapsack_1(int N, int W, int[][] item) {
        dp = new long[N+1][W+1];

        for(int i=1; i<=N; ++i) {
            for(int w=1; w<=W; ++w) {
                if(item[i][0] <= w) {
                    dp[i][w] = Math.max(dp[i-1][w], dp[i-1][w-item[i][0]] + item[i][1]);
//                    dp[i][w] = myMax(dp[i-1][w], dp[i-1][w-item[i][0]] + item[i][1]);
                }
                else {
                    dp[i][w] = dp[i-1][w];
                }
            }
        }

//        d0(dp);

        return dp[N][W];
    }
}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int w = scanner.nextInt();
        int[] weights = new int[n];
        int[] values = new int[n];

        for (int i = 0; i < n; i++) {
            weights[i] = scanner.nextInt();
            values[i] = scanner.nextInt();
        }

        long[][] dp = new long[2][n];
        System.out.println(maxValue(weights, values, dp, w, n - 1));
    }

    public static long maxValue(int[] weights, int[] values, long[][] dp, int w, int n) {

        if (n == -1) {
            return 0;
        }

        long rp = -1;
            //放
        if (weights[n] <= w) {
            if (dp[0][n] > 0) {
                rp = dp[0][n];
            } else {
                rp = maxValue(weights, values, dp, w - weights[n], n - 1) + values[n];
            }
        }
            //不放
        long rnp = -1;
        if (dp[1][n] > 0) {
            rnp = dp[1][n];
        }else {
            rnp = maxValue(weights, values, dp, w, n - 1);
        }

        if (rp > rnp) {
            dp[0][n] = rp;
            return rp;
        } else {
            dp[1][n] = rnp;
            return rnp;
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    private static final BufferedReader READER = new BufferedReader(new InputStreamReader(System.in));

    private static int getInt() {
        try {
            return Integer.parseInt(READER.readLine());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static int[] getIntArr(int n) {
        int[] ans = new int[n];

        StringTokenizer stringTokenizer = null;
        try {
            stringTokenizer = new StringTokenizer(READER.readLine(), " ");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        for (int i = 0; i < n && stringTokenizer.hasMoreTokens(); i++) {
            ans[i] = Integer.parseInt(stringTokenizer.nextToken());
        }

        return ans;
    }

    private static long solve() {
        int[] nw = getIntArr(2);
        int n = nw[0];
        int w = nw[1];

        Item[] items = new Item[n];
        for (int i = 0; i < n; i++) {
            items[i] = Item.of(getIntArr(2));
        }
        return solveInternal(n, w, items);
    }

    private static long solveInternal(int n, int W, Item[] items) {
        long[][] dp = new long[n + 1][W + 1];

        for (int i = 1; i <= n; i++) {
            for (int w = 1; w <= W; w++) {
                Item item = items[i - 1];
                if (item.w > w) {
                    dp[i][w] = dp[i - 1][w];
                } else {
                    dp[i][w] = Math.max(dp[i - 1][w], dp[i - 1][w - item.w] + item.v);
                }
            }
        }

//        for (int i = 0; i <= n; i++) {
//            for (int w = 0; w <= W; w++) {
//                System.out.println(String.format("dp[%s,%s]=%s", i, w, dp[i][w]));
//            }
//        }

        return dp[n][W];
    }

    public static final class Item {
        public final int v;
        public final int w;

        private Item(int w, int v) {
            this.v = v;
            this.w = w;
        }

        public static Item of(int[] arr) {
            return new Item(arr[0], arr[1]);
        }
    }

    public static void main(String... args) {
        System.out.println(solve());
//        System.out.println(
//                solveInternal(6, 5, new Item[]{
//                        new Item(1, 1000000000),
//                        new Item(1, 1000000000),
//                        new Item(1, 1000000000),
//                        new Item(1, 1000000000),
//                        new Item(1, 1000000000)
//                })
//        );
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
	static FastReader input = new FastReader();
	static PrintWriter out = new PrintWriter(System.out);

	static int n, w;
	static int[][] wV;
	static long[][] memo;
	static final int  _INF = (int)-1e9;

	static long solve (int n, int w) {

		if (n<0) return 0;

		if (w<0)
			return _INF;

		if (memo[n][w]!=-1) return memo[n][w];
		long x  = wV[n][1]+solve(n-1, w-wV[n][0]);
		long y  = solve(n-1, w);

		return memo[n][w] = Math.max(x, y);

	}

	public static void main(String[] args) throws NumberFormatException, IOException {

		n = input.nextInt();
		w = input.nextInt();

		wV = new int[n][2];

		for (int i = 0; i < n; i++) {
			wV[i][0] = input.nextInt();
			wV[i][1] = input.nextInt();
		}

		memo = new long[n + 5][(int) 1e5 + 100];

		for (int i = 0; i < memo.length; i++)
			Arrays.fill(memo[i], -1);

		System.out.println(solve (n-1,w));
	}

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() throws IOException {
			while (st == null || !st.hasMoreElements()) {
				st = new StringTokenizer(br.readLine());
			}
			return st.nextToken();
		}

		int nextInt() throws NumberFormatException, IOException {
			return Integer.parseInt(next());
		}

		long nextLong() throws NumberFormatException, IOException {
			return Long.parseLong(next());
		}

		double nextDouble() throws NumberFormatException, IOException {
			return Double.parseDouble(next());
		}

		String nextLine() throws IOException {
			String str = "";
			str = br.readLine();
			return str;
		}
	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastScanner in = new FastScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        D solver = new D();
        solver.solve(1, in, out);
        out.close();
    }

    static class D {
        public void solve(int testNumber, FastScanner in, PrintWriter out) {
            int n = in.ni(), W = in.ni();
            int[] w = new int[n];
            int[] v = new int[n];
            for (int i = 0; i < n; i++) {
                w[i] = in.ni();
                v[i] = in.ni();
            }
            long[][] dp = new long[n + 1][W + 1];
            for (int i = 0; i < n; i++) {
                for (int ww = 0; ww < W; ww++) {
                    dp[i + 1][ww] = Math.max(dp[i + 1][ww], dp[i][ww]);
                    if (ww + w[i] <= W)
                        dp[i + 1][ww + w[i]] = Math.max(dp[i + 1][ww + w[i]], dp[i][ww] + v[i]);
                }
            }
            long max = 0;
            for (int ww = 0; ww <= W; ww++) {
                max = Math.max(max, dp[n][ww]);
            }
            out.println(max);

        }

    }

    static class FastScanner {
        private BufferedReader in;
        private StringTokenizer st;

        public FastScanner(InputStream stream) {
            in = new BufferedReader(new InputStreamReader(stream));
        }

        public String ns() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    String rl = in.readLine();
                    if (rl == null) {
                        return null;
                    }
                    st = new StringTokenizer(rl);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return st.nextToken();
        }

        public int ni() {
            return Integer.parseInt(ns());
        }

    }
}


import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    int N = scanner.nextInt();
    int W = scanner.nextInt();
    int[] w = new int[N + 1];
    int[] v = new int[N + 1];
    for (int i = 1; i <= N; i++) {
      w[i] = scanner.nextInt();
      v[i] = scanner.nextInt();
    }

    long[][] dp = new long[N + 1][W + 1];
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j <= W; j++) {
        dp[i][j] = dp[i - 1][j];
        if (j >= w[i]) dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - w[i]] + v[i]);
      }
    }
    System.out.println(dp[N][W]);
  }
}


import java.io.*;
import java.util.*;
import java.math.*;
import java.math.BigInteger;
class Main
{
    public static void main(String args[])
    {
        StringBuilder ans=new StringBuilder();
        FastReader in=new FastReader();
        String X=in.next();
        String Y=in.next();
        int n=X.length();
        int m=Y.length();
        int dp[][]=new int[n+1][m+1];
        for(int i=0; i<=n; i++)
        {
            for(int j=0; j<=m; j++)
            {
                if(i==0 || j==0)dp[i][j]=0;
                else
                {
                    if(X.charAt(i-1)==Y.charAt(j-1))dp[i][j]=1+dp[i-1][j-1];
                    else dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);
                }

            }//System.out.println();
        }
        for(int i=0; i<m; i++)
        {
            if(dp[n][i]!=dp[n][i+1])System.out.print(Y.charAt(i));
        }
    }

    static  long pow(long a,long b)
    {

        long mod=1000000007;
        long pow=1;
        long x=a;
        while(b!=0)
        {
            if((b&1)!=0)pow=(pow*x)%mod;
            x=(x*x)%mod;
            b/=2;
        }
        return pow;
    }

    static int bits(long x)
    {
        int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1;
        return (1<<n);
    }

    static long rev(long x)
    {
        int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1;

        return ((1<<n)-1)^x;
    }
    //fucntions
    //fucntions
    //fucntions
    //fucntions
    static int[] input(int A[]) //input of Int Array
    {
        FastReader in=new FastReader();
        int N=A.length;
        for(int i=0; i<N; i++)
        {
            A[i]=in.nextInt();
        }
        return A;
    }

    static long[] input(long A[]) //Input of long Array
    {
        FastReader in=new FastReader();
        for(int i=0; i<A.length; i++)A[i]=in.nextLong();
        return A;
    }

    static long lcm(long a,long b)
    {
        return (a*b)/GCD(a,b);
    }

    static long GCD(long a,long b) //wrong output if a ||b are intially zero
    {
        if(b==0)
        {
            return a;
        }
        else return GCD(b,a%b );
    }

    static boolean isPrime(int N)
    {
        for(int i=2; i*i<N; i++)
            if(N%i==0)return false;
        return true;
    }

}
//Code For FastReader
//Code For FastReader
//Code For FastReader
//Code For FastReader
class FastReader
{
    BufferedReader br;
    StringTokenizer st;
    public FastReader()
    {
        br=new BufferedReader(new InputStreamReader(System.in));
    }

    String next()
    {
        while(st==null || !st.hasMoreElements())
        {
            try
            {
                st=new StringTokenizer(br.readLine());
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt()
    {
        return Integer.parseInt(next());
    }

    long nextLong()
    {
        return Long.parseLong(next());
    }

    double nextDouble()
    {
        return Double.parseDouble(next());
    }

    String nextLine()
    {
        String str="";
        try
        {
            str=br.readLine();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return str;
    }

}

Score : 100 points

Problem Statement

You are given strings s and t.
Find one longest string that is a subsequence of both s and t.

Notes

A subsequence of a string x is the string obtained by removing zero or more characters from x and concatenating the remaining characters without changing the order.

Constraints

s and t are strings consisting of lowercase English letters.

1 \leq |s|, |t| \leq 3000

Input

Input is given from Standard Input in the following format:

s
t

Output

Print one longest string that is a subsequence of both s and t.
If there are multiple such strings, any of them will be accepted.

Sample Input 1

axyb
abyxb

Sample Output 1

axb

The answer is axb or ayb; either will be accepted.

Sample Input 2

aa
xayaz

Sample Output 2

aa

Sample Input 3

a
z

Sample Output 3

The answer is (an empty string).

Sample Input 4

abracadabra
avadakedavra

Sample Output 4

aaadara


import java.util.*;

public class Main
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		String s1 = sc.next();
		String s2 = sc.next();

		int[][] dp = new int[s1.length()+1][s2.length()+1];

		for(int i=1;i<=s1.length();i++)
		{
			for(int j=1;j<=s2.length();j++)
			{
				dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]);

					if(s1.charAt(i-1)==s2.charAt(j-1))
					{
						dp[i][j] = 1 + dp[i-1][j-1];
					}

			}
		}

       String ans = "";
       int i =dp.length-1;
       int j =dp[0].length-1;

       while(i>=1||j>=1)
       {
       	  if(i>=1&&j>=1&&s1.charAt(i-1)==s2.charAt(j-1)&&dp[i][j]==dp[i-1][j-1]+1)
       	  {
       	  	  ans = ans + s1.charAt(i-1);
       	  }

       	  if(i>=0&&j>=1&&dp[i][j]==dp[i][j-1])
       	  {
              j=j-1;
       	  }
       	  else
       	  {
             i=i-1;
       	  }

       }
       StringBuilder str = new StringBuilder();
       str.append(ans);

       System.out.println(str.reverse());
 	}
}


import javax.swing.*;
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {
    static PrintWriter out = new PrintWriter(System.out);
    static long[] tree;
    static int m = 100000000;
    static int k1,k2;
    static boolean[] vis;
    static tuple[] tuples;
    static char[] a,b;
    public static void main(String[] args) throws IOException {
        //BufferedReader reader=new BufferedReader(new FileReader("input.txt"));
        //PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        //Scanner sc=new Scanner(System.in);
        //Reader sc = new Reader();
        //String[] in=reader.readLine().split(" ");

        a=reader.readLine().toCharArray();
        b=reader.readLine().toCharArray();
        int[][] dp=new int[a.length+1][b.length+1];
        for (int i = 1; i <=a.length ; i++) {
            for (int j = 1; j <=b.length ; j++) {
                if (a[i-1]==b[j-1])
                    dp[i][j]=dp[i-1][j-1]+1;
                else
                    dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);
            }
        }
        int i=a.length,j=b.length;
        //System.out.println(Arrays.deepToString(dp));
        //StringBuilder ans=new StringBuilder("");
        StringBuilder ans= new StringBuilder();
        while (i>0 && j>0){
            if (dp[i][j]==dp[i-1][j]){
                i--;
            }
            else if (dp[i][j]==dp[i][j-1]){
                j--;
            }
            else{
                ans.append(a[i - 1]);
                i--;
                j--;
            }
        }
        System.out.println(ans.reverse());

        out.close();
    }
    static Integer[][] memo ;
    static int lcs(int i,int j){
        if (i==a.length || j==b.length)
            return 0;
        else if (memo[i][j]!=null)return memo[i][j];
        else if (a[i]==b[j])
            return memo[i][j]=1+lcs(i+1,j+1);
        else
            return Math.max(lcs(i+1,j),lcs(i,j+1));
    }

    static boolean bfs(int a,int b){
        Queue<Integer> queue=new LinkedList<>();
        queue.add(a);

        while (!queue.isEmpty()){
            int now=queue.poll();
            vis[now]=true;

            for (int i = 0; i <k1 ; i++) {
                if (!vis[i]){
                    if((tuples[i].a<tuples[now].a&&tuples[now].a<tuples[i].b)
                                ||(tuples[i].a<tuples[now].b&&tuples[now].b<tuples[i].b)){
                        queue.add(i);
                        if (i==b){
                            Arrays.fill(vis,false);
                            return true;
                        }
                    }
                }
            }
        }

        Arrays.fill(vis,false);
        return false;

    }
    static int find(int[] p,int x){
        if (p[x]==x)return x;

        return p[x]=find(p,p[x]);
    }
    static void union(int[] p,int a,int b){
        p[find(p,b)]=find(p,a);
    }
    static void built(long[] a,int at,int l,int r,int n){
        if (l==r){
            tree[at]=a[l];
        }
        else {
            int mid=(l+r)/2;
            int right=2*at;
            int left=(2*at)+1;
            built(a,left,l,mid,n-1);
            built(a,right,mid+1,r,n-1);
            if (n%2==0)
                tree[at]=tree[left]^tree[right];
            else
                tree[at]=tree[left]|tree[right];
        }
    }
    static void update(int at,int l,int r,int i,long v,int n){
        if (i<l || i>r)return;

        if (l==r){
            tree[at]=v;
        }
        else {
            int mid=(l+r)/2;
            int right=2*at;
            int left=(2*at)+1;
            update(left,l,mid,i,v,n-1);
            update(right,mid+1,r,i,v,n-1);
            if (n%2==0)
                tree[at]=tree[left]^tree[right];
            else
                tree[at]=tree[left]|tree[right];
        }
    }

}

    class node implements Comparable<node> {
        Integer no;
        Integer cost;
        Vector<node> adj = new Vector<>();

        public node(Integer no, Integer cost) {
            this.no = no;
            this.cost = cost;
        }

        @Override
        public String toString() {
            return "node{" +
                    "no=" + no +
                    ", cost=" + cost +
                    '}';
        }

        @Override
        public int compareTo(node o) {
            return o.cost - this.cost;
        }
    }

    class edge implements Comparable<edge> {
        tuple u;
        Double cost;

        public edge(tuple u, Double cost) {
            this.u = u;
            this.cost = cost;
        }

        @Override
        public int compareTo(edge o) {
            return this.cost.compareTo(o.cost);
        }

        @Override
        public String toString() {
            return "edge{" +
                    "u=" + u +
                    ", cost=" + cost +
                    '}';
        }
    }

    ///*
    class tuple implements Comparable<tuple> {
        Integer a;
        Integer b;

        public tuple(Integer a, Integer b) {
            this.a = a;
            this.b = b;
        }

        @Override
        public int compareTo(tuple o) {
            return (int) (this.b - o.b);
        }

        @Override
        public String toString() {
            return "tuple{" +
                    "a=" + a +
                    ", b=" + b +
                    '}';
        }
    }

    //*/
    class Reader {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader() {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException {
            byte[] buf = new byte[64];
            int cnt = 0, c;
            while ((c = read()) != -1) {
                if (c == '\n') break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException {
            int ret = 0;
            byte c = read();
            while (c <= ' ') c = read();
            boolean neg = (c == '-');
            if (neg) c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg) return -ret;
            return ret;
        }

        public long nextLong() throws IOException {
            long ret = 0;
            byte c = read();
            while (c <= ' ') c = read();
            boolean neg = (c == '-');
            if (neg) c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg) return -ret;
            return ret;
        }

        public double nextDouble() throws IOException {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ') c = read();
            boolean neg = (c == '-');
            if (neg) c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (c == '.') while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10);
            if (neg) return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1) buffer[0] = -1;
        }

        private byte read() throws IOException {
            if (bufferPointer == bytesRead) fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException {
            if (din == null) return;
            din.close();
        }
    }


import java.io.*;
import java.util.*;

public class Main {
    static PrintWriter pw;
    static Scanner sc;
    static int inf=(int)1e9;
    static String a, b;
    static int[] arr;
    static int[][] dp;
    static HashMap<Pair, Integer> map= new HashMap<Pair, Integer>();
    static long ceildiv(long x, long y){ return (x+y-1)/y;}
    public static void main(String[] args) throws Exception {
        sc = new Scanner(System.in);
        pw = new PrintWriter(System.out);
        a=sc.nextLine(); b=sc.nextLine();
        dp=new int[a.length()][b.length()];
        for(int i=0; i<a.length(); i++)
            Arrays.fill(dp[i], -1);
        solve(0, 0);
        trace();
        pw.close();
    }
    static int solve(int i, int j){
        if(i==a.length() || j==b.length())
            return 0;
        if(dp[i][j]==-1)
            dp[i][j]=a.charAt(i)==b.charAt(j)?1+solve(i+1, j+1):Math.max(solve(i, j+1), solve(i+1, j));
        return dp[i][j];
    }
    static void trace(){
        StringBuilder ans= new StringBuilder();
        int i=0, j=0;
        while(i<a.length() && j<b.length()){
            if(a.charAt(i)==b.charAt(j)){
                ans.append(a.charAt(i++));
                j++;
            }else{
                if(i<a.length()-1 && j<b.length()-1){
                    if(dp[i+1][j]>dp[i][j+1])
                        i++;
                    else
                        j++;
                }else if(i!=a.length()-1){
                    i++;
                }else if(j!=b.length()-1){
                    j++;
                }else{
                    break;
                }
            }
        }
        pw.println(ans);
    }
    static boolean test(char[][] arr){
        for(int i=1; i<arr.length; i++){
            if(!test(arr[0], arr[i]))
                return false;
        }
        return true;
    }
    static boolean test(char[] a, char[] b){
        int diff=0;
        for(int i=0; i<a.length; i++){
            diff+=a[i]==b[i]?0:1;
            if(diff==2)
                return false;
        }
        return true;
    }
    static void printArr(int[] arr){
        for(int i=0; i<arr.length-1; i++)
            pw.print(arr[i]+" ");
        pw.println(arr[arr.length-1]);
    }
    static void printArr(long[] arr){
        for(int i=0; i<arr.length-1; i++)
            pw.print(arr[i]+" ");
        pw.println(arr[arr.length-1]);
    }
    static void printArr(Integer[] arr){
        for(int i=0; i<arr.length-1; i++)
            pw.print(arr[i]+" ");
        pw.println(arr[arr.length-1]);
    }
    static void printArr(char[] arr){
        for(int i=0; i<arr.length; i++)
            pw.print(arr[i]);
        pw.println();
    }
    static void printArr(ArrayList list){
        for(int i=0; i<list.size(); i++)
            pw.print(list.get(i)+" ");
        pw.println();
    }
    static class Scanner {
        StringTokenizer st;
        BufferedReader br;
        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public Scanner(FileReader r) {
            br = new BufferedReader(r);
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        public int[] nextArr(int n) throws IOException {
            int[] arr = new int[n];
            for (int i = 0; i < arr.length; i++)
                arr[i] = nextInt();
            return arr;
        }
        public Integer[] nextsort(int n) throws IOException{
            Integer[] arr=new Integer[n];
            for(int i=0; i<n; i++)
                arr[i]=nextInt();
            return arr;
        }
        public Pair nextPair() throws IOException{
            return new Pair(nextInt(), nextInt());
        }
        public Pair[] nextPairArr(int n) throws IOException{
            Pair[] arr=new Pair[n];
            for(int i=0; i<n; i++)
                arr[i]=new Pair(nextInt(), nextInt());
            return arr;
        }
        public boolean ready() throws IOException {
            return br.ready();
        }
    }
    static class Pair implements Comparable<Pair>{
        int x;
        int y;
        public Pair(int x, int y) {
            this.x=x;
            this.y=y;
        }
        public int hashCode() {
            return this.x*1000+this.y;
        }
        public int compareTo(Pair p) {
            int st1=p.x==0?arr[p.y+1]:arr[p.x-1];
            int end1=p.y==arr.length-1?arr[p.x-1]:arr[p.y+1];
            int st2=this.x==0?arr[this.y+1]:arr[this.x-1];
            int end2=this.y==arr.length-1?arr[this.x-1]:arr[this.y+1];
            int a=st1%2==end1%2?1:0;
            int b=st2%2==end2%2?1:0;
            if(a>b)
                return 1;
            if(a<b)
                return -1;
            return this.y-this.x-p.y+p.x;
        }
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (this.getClass() != obj.getClass()) {
                return false;
            }
            Pair p = (Pair) obj;
            return this.x==p.x && this.y==p.y;
        }
        public boolean equal(Pair p){
            if(this.x==p.x)
                return this.y==p.y;
            if(this.x==p.y)
                return this.y==p.x;
            else
                return false;
        }
        public Pair clone(){
            return new Pair(x, y);
        }
        public String toString(){
            return this.x+" "+this.y;
        }
    }
    static class Triple implements Comparable<Triple>{
        int x;
        int y;
        char c;
        public Triple(int x, int y, char c){
            this.x=x;
            this.y=y;
            this.c=c;
        }
        public int compareTo(Triple t){
            return this.y-t.y;
        }
        public String toString(){
            if(this.x==1)
                return "INSERT "+(this.y+1)+" "+this.c;
            else if(this.x==0)
                return "REPLACE "+(this.y+1)+" "+this.c;
            else
                return "DELETE "+(this.y+1);
        }
    }
    static class decimal{
        int x;
        int dec;
        public decimal(String s){
            x=Integer.parseInt(s.substring(0, s.length()-3));
            dec=Integer.parseInt(s.substring(s.length()-2));
        }
        public decimal(int x, int dec){
            this.x=x;
            this.dec=dec;
        }
        public decimal plus(decimal a){
            return new decimal(this.x+a.x+(this.dec+a.dec)/100, (this.dec+a.dec)%100);
        }
        public decimal minus(decimal a){
            return new decimal(this.x-a.x-(this.dec>=a.dec?0:1), (this.dec+100-a.dec)%100);
        }
        public decimal min(decimal a){
            if(a.x>this.x)
                return this;
            if(this.x>a.x)
                return a;
            if(a.dec>this.dec)
                return this;
            return a;
        }
        public String toString(){
            return this.x+"."+this.dec;
        }
        public decimal clone(){
            return new decimal(this.x, this.dec);
        }
        public boolean equal(decimal a){
            return a.x==this.x && a.dec==this.dec;
        }
    }
}


import java.util.*;
import java.io.*;

public class Main {
	static int dp[][] = new int[3005][3006];
	static String ans = "";
	public static int lcs(char p[] , char s[] , int n , int m) {
		if(n == 0 || m == 0)
			return 0;

		if(dp[n][m] != -1) return dp[n][m];

		else if(p[n-1] == s[m-1]) {
			return dp[n][m] = 1 + lcs(p , s , n-1 , m-1);
		}

		int temp1 = lcs(p , s , n-1 , m);
		int temp2 = lcs(p , s , n , m-1);
		return dp[n][m] = Math.max(temp1, temp2);
	}
	public static void pr(char p[] , char s[] ,int n , int m) {
		if(n<=0 || m <= 0)
			return;
		if(p[n-1] == s[m-1]) {
			ans = p[n-1] + ans;
			pr(p,s,n-1,m-1);
		}
		else if(dp[n-1][m] > dp[n][m-1])
			pr(p,s,n-1,m);
		else
			pr(p,s,n,m-1);
	}
	public static void solve(InputReader in) {
		char p[] = in.readString().toCharArray();
		char s[] = in.readString().toCharArray();
		int n = p.length , m = s.length;
		for(int i = 0; i<3005; i++) {
			for(int j = 0; j<3006; j++) {
				dp[i][j] = -1;
			}
		}
		lcs(p,s,n,m);
		pr(p,s,n,m);
		System.out.println(ans);
	}

	public static void main(String[] args) {
		InputReader in = new InputReader(System.in);
		int t = 1;
		while(t-- > 0) {
			solve(in);
		}
	}
}

class InputReader{
	private InputStream stream;
	private byte[] buf = new byte[1024];
	private int curChar;
	private int numChars;
	private SpaceCharFilter filter;

	 public InputReader(InputStream stream) {
		this.stream = stream;
	}

     public int read() {
		if (numChars == -1)
			throw new InputMismatchException();
		if (curChar >= numChars) {
			curChar = 0;
			try {
				numChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (numChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

     public int readInt() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

     public String readString() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isSpaceChar(c));
		return res.toString();
	}

     public long readLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			if (c < '0' || c > '9') {
				throw new InputMismatchException();
			}
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

     public boolean isSpaceChar(int c) {
		if (filter != null)
			return filter.isSpaceChar(c);
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

     public String next() {
		return readString();
	}

     public interface SpaceCharFilter {
		public boolean isSpaceChar(int ch);
	}
}


import java.util.*;

class Main {

    String s;
    String t;

    private void run() {

        Scanner sc = new Scanner(System.in);
        s = sc.next();
        t = sc.next();

        int S = s.length();
        int T = t.length();

        int[][] dp = new int[S+1][T+1];
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i <= S; i++) {
            for (int j = 0; j <= T; j++) {
                if (i == 0 || j == 0) dp[i][j] = 0;
                else if (s.charAt(i-1) == t.charAt(j-1)) {
                    sb.append(s.charAt(i-1));
                    dp[i][j] = dp[i-1][j-1] + 1;
                } else if (dp[i-1][j] > dp[i][j-1]) {
                    dp[i][j] = dp[i-1][j];
                } else {
                    dp[i][j] = dp[i][j-1];
                }
            }
        }

        System.out.println(sb.substring(sb.length()/2));
    }

    public static void main(String[] args) {
        Main solver = new Main();
        solver.run();
    }
}


import java.util.*;
import java.io.*;
import java.lang.*;

 class Main{

    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    static StringBuilder sb = new StringBuilder("");
    static StringTokenizer stk = new StringTokenizer("");

    public static void main(String args[]){
        try{
            String s = bf.readLine();
            String t = bf.readLine();
            solver(s,t);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static void solver(String s, String t){
        int[][] dp = new int[s.length()][t.length()];

        for(int i=0;i<s.length();i++){ //for string s
            for(int j=0;j<t.length();j++){//for string t

                if(s.charAt(i) == t.charAt(j)){
                    dp[i][j] = 1 + ((i-1 >= 0 && j-1 >= 0) ? dp[i-1][j-1] : 0);
                }else{
                    dp[i][j] = Math.max((i-1 >= 0 ? dp[i-1][j] : 0),(j-1 >= 0 ? dp[i][j-1] : 0));
                }

            }
        }
        int slen = s.length()-1;
        int tlen = t.length()-1;

        StringBuilder sb  = new StringBuilder("");

        while(slen >= 0 && tlen >= 0){

            if((slen >= 0 && tlen >= 0) && s.charAt(slen) == t.charAt(tlen)){
                sb.append(s.charAt(slen));
                slen--;
                tlen--;
            }else{
                if(slen-1 >= 0 && tlen-1 >= 0){
                    if(dp[slen-1][tlen] > dp[slen][tlen-1] ){
                        slen--;
                    }else{
                        tlen--;
                    }
                }else if(slen-1 >= 0 && tlen-1 < 0){
                    slen --;
                }else if(slen-1 < 0 && tlen-1 >= 0){
                    tlen --;
                }
            }
        }

        // printDpTable(dp);
        // System.out.println(dp[s.length()-1][t.length()-1]);
        System.out.println(sb.reverse().toString());
    }

    public static void printDpTable(int[][] dp){
        for(int i=0;i<dp.length;i++){
            for(int j=0;j<dp[i].length;j++){
                System.out.print(dp[i][j]+" ");
            }
            System.out.println("");
        }
    }
}


// Problem : F - LCS
// Contest : Educational DP Contest
// URL : https://atcoder.jp/contests/dp/tasks/dp_f
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

import java.io.*;
import java.util.*;

public class Main{

	public static void main(String[] args) throws Exception {
		FastReader scan = new FastReader();
		PrintWriter out = new PrintWriter(System.out);
		//PrintWriter out = new PrintWriter("file.out");
		Task solver = new Task();
		//int t = scan.nextInt();
		int t = 1;
		for(int i = 1; i <= t; i++) solver.solve(i, scan, out);
		out.close();
	}

	static class Task {
		static final int inf = Integer.MAX_VALUE;
		public void solve(int testNumber, FastReader sc, PrintWriter pw) {
			String str = sc.nextLine();
			String str2 = sc.nextLine();
			int n = str.length();
			int m = str2.length();
			int[][] dp = new int[str.length()][str2.length()]; //dp[i][j]= longest subsequence of substr(i) and substr(j)
			for(int i=0;i<str.length();i++){
				for(int j=0;j<str2.length();j++){
					if(str.charAt(i)==str2.charAt(j)){
						if(i==0 || j==0) dp[i][j] = 1;
						else dp[i][j] = dp[i-1][j-1] + 1;
					}
					if(i>0)
					dp[i][j] = Math.max(dp[i][j], dp[i-1][j]);
					if(j>0)
					dp[i][j] = Math.max(dp[i][j], dp[i][j-1]);
				}
			}
			StringBuilder sb = new StringBuilder();
			int i = n-1;
			int j = m-1;
			for(;i>=0&&j>=0;){
				if(str.charAt(i)==str2.charAt(j)){
					sb.append(str.charAt(i));
					i--;j--;
				}
				else if(j==0||i>0&&dp[i-1][j]>dp[i][j-1]){
					i--;
				}
				else{
					j--;
				}
			}
			sb=sb.reverse();
			pw.println(sb);

		}
	}
	long binpow(long a, long b, long m) {
		a %= m;
		long res = 1;
		while (b > 0) {
			if ((b & 1) == 1)
				res = res * a % m;
			a = a * a % m;
			b >>= 1;
		}
		return res;
	}
	static class tup implements Comparable<tup>{
		int a, b;
		tup(int a,int b){
			this.a=a;
			this.b=b;
		}
		@Override
		public int compareTo(tup o){
			return Integer.compare(o.b,b);
		}
	}
	static void shuffle(int[] a) {
		Random get = new Random();
		for (int i = 0; i < a.length; i++) {
			int r = get.nextInt(a.length);
			int temp = a[i];
			a[i] = a[r];
			a[r] = temp;
		}
	}

	static void shuffle(long[] a) {
		Random get = new Random();
		for (int i = 0; i < a.length; i++) {
			int r = get.nextInt(a.length);
			long temp = a[i];
			a[i] = a[r];
			a[r] = temp;
		}
	}

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		public FastReader(String s) throws FileNotFoundException {
			br = new BufferedReader(new FileReader(new File(s)));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}

}


import java.util.*;
public class Main {
    public static void main(String [] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        String t = sc.next();
        int DP[][] = new int[s.length()+1][t.length()+1];
        int L [][] = new int[s.length()+1][t.length()+1];
        for(int i=0;i<s.length()+1;i++) {
            Arrays.fill(DP[i],0);
            Arrays.fill(L[i],0);
        }
        for(int i=1;i<s.length()+1;i++){
            for(int j=1;j<t.length()+1;j++){
                if(s.charAt(i-1)==t.charAt(j-1)){
                    DP[i][j] = DP[i-1][j-1]+1;
                    L[i][j] = 2;
                }
                else {
                    DP[i][j] = Math.max(DP[i-1][j],DP[i][j-1]);
                    if(DP[i][j]==DP[i-1][j])L[i][j] =1;
                    else L[i][j]=3;
                }
            }
        }
        StringBuilder opt = new StringBuilder();
        int i = s.length(), j = t.length();
        for(int k=0;k<s.length()*t.length();k++){
            if(s.charAt(i-1)==t.charAt(j-1)) opt.insert(0,s.charAt(i-1));
            if(L[i][j]==1) i--;
            else if(L[i][j]==3) j--;
            else if(L[i][j]==2){
                i--;
                j--;
            }
            if(i==0||j==0) break;
        }
        System.out.println(opt);
    }
}


import java.io.*;
import java.util.*;

public class Main {
    //static String probName = "template";
    static Comparator<int[]> comp = new Comparator<int[]>() {
        public int compare(int[] a, int[] b) {
            for (int i=0; i<a.length; i++) {
                if (a[i] < b[i])       return -1;
                else if (a[i] > b[i])  return 1;
            }
            return 0;
        }
    };
    static final long LARGE_LONG = 1000000000000000000L;
    static final int LARGE_INT = 1000000007;
    static final int[] dx = new int[] {1, -1, 0, 0};
    static final int[] dy = new int[] {0, 0, 1, -1};
    static int m;
    static int n;
    static char[] s;
    static char[] t;
    static int[][][] lcs;
    //lcs[i][j][0] = length of lcs occuring in s before i and in t before j. [1] is pointer for reconstructing

    public static void main(String[] argst) throws IOException {
        FastReader in = new FastReader();
        // FastReader in = new FastReader(probName + ".in");
        // Scanner in = new Scanner(System.in);

        // PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(probName + ".out")));
        PrintWriter out = new PrintWriter(System.out);
        s = in.next().toCharArray();
        t = in.next().toCharArray();
        m = s.length;
        n = t.length;
        lcs = new int[m+1][n+1][2];
        int mx = -1;
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                int pla = -1;
                if (s[i-1] == t[j-1]) {
                    pla = lcs[i-1][j-1][0] + 1;
                    lcs[i][j][0] = pla;
                    lcs[i][j][1] = 1;

                }
                else {
                    int plal = lcs[i-1][j][0];
                    int plar = lcs[i][j-1][0];
                    if (plal > plar) {
                        pla = plal;
                        lcs[i][j][0] = plal;
                        lcs[i][j][1] = 2;
                    }
                    else {
                        pla = plar;
                        lcs[i][j][0] = plar;
                        lcs[i][j][1] = 3;
                    }
                }
                if (pla > mx) {
                    mx = pla;
                }
            }
        }
        int si = 0;
        int sj = 0;
    dumb:
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (lcs[i][j][0] == mx) {
                    si = i;
                    sj = j;
                    break dumb;
                }
            }
        }
        //System.out.println(Arrays.deepToString(lcs));
        ArrayList<Character> ll = new ArrayList<Character>();
        int count = 0;
        while (count < mx) {
            if (lcs[si][sj][1] == 1) {
                ll.add(s[si-1]);
                si--;
                sj--;
                count ++;
            }
            else if (lcs[si][sj][1] == 2) {
                si--;
            }
            else if (lcs[si][sj][1] == 3) {
                sj--;
            }
        }
        int plan = ll.size()-1;
        for (int i = plan; i >= 0; i--) {
            out.print(ll.get(i));
        }
        out.println();
        out.close();
    }

    /* FastReader code from  https://www.geeksforgeeks.org/fast-io-in-java-in-competitive-programming/
       nextLine modified, and added next(), and hasNext()
    */
    static class FastReader {
        final private int BUFFER_SIZE = 1 << 16;
        final private int MAX_LINE_SIZE = 1 << 20;
        private DataInputStream din;
        private byte[] buffer, lineBuf;
        private int bufferPointer, bytesRead;

        public FastReader() {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }
        public FastReader(String file_name) throws IOException {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }
        public boolean hasNext() throws IOException {
            byte c;
            while ((c = read()) != -1) {
                if (c > ' ') {
                    bufferPointer--;
                    return true;
                }
            }
            return false;
        }
        public String nextLine() throws IOException {
            if (lineBuf == null)	lineBuf = new byte[MAX_LINE_SIZE];
            int ctr = 0;
            byte c;
            while ((c = read()) != -1) {
                if (c == '\r')        continue;
                if (c == '\n')        break;
                lineBuf[ctr++] = c;
            }
            return new String(lineBuf, 0, ctr);
        }
        public String next() throws IOException {
            if (lineBuf == null)	lineBuf = new byte[MAX_LINE_SIZE];
            int ctr = 0;
            byte c = read();
            while (c <= ' ')  	c = read();
            while (c > ' ') {
                lineBuf[ctr++] = c;
                c = read();
            }
            return new String(lineBuf, 0, ctr);
        }
        public int nextInt() throws IOException {
            int ret = 0;
            byte c = read();
            while (c <= ' ')   c = read();
            boolean neg = (c == '-');
            if (neg)           c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');

            if (neg)           return -ret;
            return ret;
        }
        public long nextLong() throws IOException {
            long ret = 0;
            byte c = read();
            while (c <= ' ')    c = read();
            boolean neg = (c == '-');
            if (neg)            c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg)            return -ret;
            return ret;
        }
        public double nextDouble() throws IOException {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)  	c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (c == '.') {
                while ((c = read()) >= '0' && c <= '9') {
                    ret += (c - '0') / (div *= 10);
                }
            }
            if (neg)     return -ret;
            return ret;
        }
        private void fillBuffer() throws IOException {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }
        private byte read() throws IOException {
            if (bufferPointer == bytesRead)     fillBuffer();
            return buffer[bufferPointer++];
        }
        public void close() throws IOException {
            if (din == null) 	   return;
            din.close();
        }
    }
}


/*
If you want to aim high, aim high
Don't let that studying and grades consume you
Just live life young
******************************
If I'm the sun, you're the moon
Because when I go up, you go down
*******************************
I'm working for the day I will surpass you
https://www.a2oj.com/Ladder16.html
*/
import java.util.*;
import java.io.*;
import java.math.*;

   public class Main
   {
      public static void main(String omkar[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
         String s = infile.readLine();
         String t = infile.readLine();/*
         int N = s.length();
         int M = s.length();
         Node[][] dp = new Node[N+1][M+1];
         for(int i=0; i <= N; i++)
            for(int j=0; j <= M; j++)
               dp[i][j] = new Node(-1, -1, 0);
         for(int i=1; i <= N; i++)
            for(int j=1; j <= M; j++)
            {
               if(s.charAt(i-1) == t.charAt(j-1))
               {
                  int len = dp[i-1][j-1].length+1;
                  if(len > dp[i][j].length)
                     dp[i][j] = new Node(i-1, j-1, len);
               }
               else
               {
                  int len = dp[i][j-1].length;
                  if(len > dp[i][j].length)
                     dp[i][j] = new Node(i, j-1, len);
                  len = dp[i-1][j].length;
                  if(len > dp[i][j].length)
                     dp[i][j] = new Node(i-1, j, len);
               }
            }*/
         System.out.println(solve(s,t));
      }
      public static String solve(String s, String t)
      {
   		if(s.length() == 0 || t.length() == 0)
            return "";
   		int m = s.length();
   		int n = t.length();
   		int[][] dp = new int[m+1][n+1];
   		for (int i = 1; i <= m; i++)
   			for (int j = 1; j <= n; j++)
            {
   				if(s.charAt(i-1) == t.charAt(j-1))
                  dp[i][j] = dp[i-1][j-1] + 1;
   				else
                  dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
   			}
   		int i = m, j = n;
   		StringBuilder sb = new StringBuilder();
   		while(i >= 1 && j >= 1)
         {
   			if(dp[i][j] == dp[i][j-1])
               j--;
   			else if(dp[i][j] == dp[i-1][j])
               i--;
   			else
            {
   				sb.append(s.charAt(i-1));
   				i--;
   				j--;
   			}
   		}
   		return sb.reverse().toString();
   	}
   }
   class Node
   {
      public int length;
      public int x;
      public int y;

      public Node(int a, int b, int c)
      {
         x = a;
         y = b;
         length = c;
      }
   }


import javax.swing.*;
import java.io.*;
import java.util.*;

public class Main {

    static class Point {
        int i;
        int j;

        public Point(int i, int j) {
            this.i = i;
            this.j = j;
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
        String first = rd.readLine();
        String second = rd.readLine();
        int[][] arr = new int[first.length()][second.length()];
        Point[][] arr2 = new Point[first.length()][second.length()];
        if (first.charAt(0) == second.charAt(0)) {
            arr[0][0] = 1;
        } else {
            arr[0][0] = 0;
        }
        arr2[0][0] = new Point(0,0);
        for(int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[0].length; j++) {
                int prev = 0;
                if(i > 0) {
                    prev = arr[i - 1][j];
                }
                int prev2 = 0;
                if(j > 0) {
                    prev2 = arr[i][j- 1];
                }
                int curValue = 0;
                if (first.charAt(i) == second.charAt(j)) {
                    if (i > 0 && j > 0) {
                        arr[i][j] = arr[i - 1][j - 1] + 1;
                        arr2[i][j] = new Point(i-1,j - 1);
                    } else {
                        arr[i][j] = 1;
                        if (i == 0) {
                            arr2[i][j] = new Point(0, j -1);
                        } else {
                            arr2[i][j] = new Point(i - 1, 0);
                        }
                    }
                } else {
                    arr[i][j] = Integer.max(prev, prev2);
                    if (arr[i][j] == prev) {
                        arr2[i][j] = new Point(i-1,j);
                    } else if (arr[i][j] == prev2) {
                        arr2[i][j] = new Point(i,j - 1);
                    }
                }
            }
        }

        StringBuilder s = new StringBuilder();
        int lastI = arr.length - 1;
        int lastJ = arr[0].length - 1;
        while (true) {
            if (first.charAt(lastI) == second.charAt(lastJ)) {
                s.append(first.charAt(lastI));
            }
           Point p = arr2[lastI][lastJ];
            if (p.i == -1 || p.j == -1) {
                break;
            }
           lastI = p.i;
           lastJ = p.j;
           if (lastI == 0 && lastJ == 0) {
               if (first.charAt(lastI) == second.charAt(lastJ)) {
                   s.append(first.charAt(lastI));
               }
               break;
           }
        }
        BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(System.out));
        wr.append(s.reverse());
        wr.flush();
    }

}