// ਅਗਮਜੋਤ ਸਿੰਘ
// AGAMJOT SINGH

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

// a_gamer

int power(int x, unsigned int y, int p) {
    int res = 1;      // Initialize result

    x = x % p;  // Update x if it is more than or
                // equal to p

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;

        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}

int max(int a, int b){
    if (a > b)
        return a;
    else
        return b;
}
int maxSubArraySum(vector<int> &v){
    int size = v.size();
    int max_so_far = 0, max_ending_here = 0;
    int a[size];
    for (int i = 0; i < size; i++)
    {
        a[i] = v[i];
    }
    for (int i = 0; i < size; i++)
    {
        max_ending_here = max_ending_here + a[i];
        if (max_ending_here < 0)
            max_ending_here = 0;
        else if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;
    }
    return max_so_far;
}
int NcR(int n, int r) {

    long long p = 1, k = 1;
    if (n - r < r)
        r = n - r;
    if (r != 0) {
        while (r) {
            p *= n;
            k *= r;
            long long m = __gcd(p, k);
            p /= m;
            k /= m;
            n--;
            r--;
        }
    }
    else
        p = 1;
    return p;
}

signed main(){
    fastio;
    int n, k;
    cin >> n >> k;
    int arr[n];
    for(int i=0;i<n;i++){
        cin >> arr[i];
    }
    int dp[n];
    for(int i=0;i<n;i++){
        if(i == 0){
            dp[i] = 0;
        }
        else{
            // loop of k
            int temp = k , idx = i - 1 , mn = INT64_MAX;
            while(temp-- && idx>=0){
                mn =  min(abs(arr[idx] - arr[i]) + dp[idx], mn);
                idx--;
            }
            dp[i] = mn;
        }
    }
    cout << dp[n-1] << endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <numeric>
#include <queue>
#include <sstream>

using namespace std;

typedef long long ll;

#define rep(i,n) for(int i=0;i<(n);i++)

vector<ll> cost(999999,-1);
vector<ll> memo(999999,-1);
ll K,a;

ll dp(ll n){
    ll c=K,check=1e9+7;
    if(n==0) return 0;
    if(memo[n]!=-1) return memo[n];
    else if(n<K) c=n;
    for(ll i=1;i<=c;i++){
        a=dp(n-i)+abs(cost[n]-cost[n-i]);
        if(check>a)check=a;
    }
    return memo[n]=check;
}

int main(){
    int N;
    cin>>N>>K;
    rep(i,N) cin>>cost[i];
    memo[0]=0;
    cout<<dp(N-1)<<endl;
    return 0;
}

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.


#include<iostream>
#include<bits/stdc++.h>

using namespace std;

int main()
{
  int n,k;
  cin>>n>>k;
  int arr[n],count[n],x;
  for(int i=0;i<n;i++)
  {
    cin>>arr[i];
  }
  if(n<k)
    cout<<0<<endl;
  else
  {
    count[0] = 0;
  count[1] = abs(arr[0]-arr[1]);
  for(int i=2;i<k;i++)
  {
  	x = 100000000;
  	for(int j=0;j<i;j++)
  	{
  		int y = abs(arr[j]-arr[i])+count[j];
  		if(y<x)
  			x = y;
	}
	count[i] = x;
  }
  for(int i=k;i<n;i++)
  {
    x = 100000000;
  	for(int j=i-k;j<i;j++)
  	{
  		int y = abs(arr[j]-arr[i])+count[j];
  		if(y<x)
  			x = y;
	}
	count[i] = x;
  }
  cout<<count[n-1]<<endl;

}
return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,j,k;
    int a[100005],dp[100005];
    cin>>n>>k;
    for(i=0;i<n;i++)
    cin>>a[i];
    dp[0]=0;
    for(i=1;i<n;i++)
    dp[i]=INT_MAX;
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<=i+k;j++)
        {
            if(j<n)
            dp[j]=min(dp[j],dp[i]+abs(a[j]-a[i]));
        }
    }
    cout<<dp[n-1];
    return 0;
}


#include <iostream>
using namespace std;

const int MAX = 1e9;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int k;
	long int n;
	cin>>n>>k;
	int h[n];
	for(int i = 0; i < n; i++) {
		cin>>h[i];
	}
	int cost[n] = {MAX};
	cost[0] = 0;
	cost[1] = abs(h[1] - h[0]);

	for(int i = 2; i < n; i++) {
		for(int j = 1; j <=k; j++) {
			if(i-j>=0)
		cost[i] = std::min(cost[i],cost[i-j]+abs(h[i] -h[i-j]));
	}

}
		cout<<cost[n-1]<<"\n";
	return 0;
}


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

int main(){
  int n,k,*dp,temp;
  vector<int> arr;
  if(scanf("%d %d",&n,&k)!=2) printf("");

  dp=(int*)malloc((n+1)*sizeof(int));
  arr.push_back(-1);

  for(int i=0;i<n;i++){
    if(scanf("%d",&temp)!=1) printf("");
    arr.push_back(temp);
  }

  if(n==1){
    printf("0\n");
    return 0;
  }

  if(n==2){
    printf("%d\n",abs(arr[2]-arr[1]));
    return 0;
  }

  dp[0]=-1;
  dp[1]=0;
  dp[2]=abs(arr[2]-arr[1]);

  for(int i=3;i<=n;i++){
    temp=INT_MAX;
  	for(int j=i-k;j<i;j++){
    	temp=min(dp[j]+abs(arr[j]-arr[i]),temp);
    }
    dp[i]=temp;
  }

  printf("%d\n",dp[n]);

  return 0;
}


#include <bits/stdc++.h>
#define em3 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
#define X real()
#define Y imag()
#define cross(a, b) ((conj(a) * (b)).Y)
#define dot(a, b) ((conj(a) * (b)).X)
#define EPS 1e-7
long long mod = 1e9 +7;
typedef long long ll;
typedef long double ld;
const double pi = acos(-1);
typedef complex <double> point;
typedef tuple<int, int, int> line;
typedef vector<point> polygon;

int main()
{

    em3
    int n , h[100005] , ans[100005] , x;
    cin >> n >> x;
    for(int i = 0 ; i < n ; i++)
        cin >> h[i];
    ans[0] = 0, ans[1] = abs(h[1] - h[0]);
    for(int i = 2 ; i < n ; i++)
    {
        ans[i] = 1e9;
        for(int j = i - 1 ; j >= max(0 , i - x) ; j--)
            ans[i] = min(ans[i] , ans[j] + (abs(h[i] - h[j])));
    }
    cout << ans[n - 1] << endl;

    return 0;
}


    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long int
    #define pb push_back
    #define mk make_pair
    #define ft first
    #define sc second
    #define ppb pop_back
    #define sz size
    #define forn(i,a,n) for(i=a;i<n;i++)
    #define M 1000000007
    #define inf 2e18

    int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll t,k;
    cin>>t>>k;
    ll i, arr[t];
    for(i=0;i<t;i++)
    	cin>>arr[i];
    ll dp[t],j;
    memset(dp,0,sizeof dp);
    dp[1]=abs(arr[1]-arr[0]);
    for(i=2;i<t;i++)
    	{
    	dp[i]=inf;
    	for(j=1;j<=k&&(i-j)>=0;j++)
    	dp[i]=min(dp[i],dp[i-j]+abs(arr[i]-arr[i-j]));
    }
    cout<<dp[t-1];
    return 0;
    }


#include <iostream>
using namespace std;

template<class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr int MAX_N = 1e5+100+7;
constexpr int MAX_H = 1e4+7;

int h[MAX_N];
long long dp[MAX_N];

int main() {
  int N, K; cin >> N >> K;
  for (int i = 0; i < N; ++i) cin >> h[i];

  // 初期化 (最小化問題なので MAX_H に初期化)
  for (int i = 0; i < 100100; ++i) dp[i] = MAX_H;

  // 初期条件
  dp[0] = 0;

  // ループ
  for (int i = 0; i < N; ++i) {
    for (int j = 1; j <= K; ++j) {
      chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
    }
  }

  cout << dp[N-1] << endl;
  return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#define Endl endl
#define mp make_pair
#define rep(N) for(int i=0;i<N;i++)
#define repj(N) for(int j=0;j<N;j++)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define For(I,N) for(int I=0;I<N;I++)
#define cinone(N) int N;cin>>N;
#define scanfone(N) int N;cin>>N;
#define cinng(N,M) int N[M];for(int yiuytvnm=0;yiuytvnm<M;yiuytvnm++) cin>>N[yiuytvnm];
#define scanfng(N,M) int N[M];for(int qrwuoiq=0;qrwuoiq<M;qrwuoiq++) scanf("%d",&N[qrwuoiq]);
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
#define ceil(a,b) ((a-1)/b+1)
typedef unsigned long long ull;
const int inf=1039074182;
using namespace std;
int dp[100005];
int n,k;
int a[100005];

int main()
{
//	freopen("input.txt","r",stdin);
	memset(dp,0x3f,sizeof(dp));
	ios_base::sync_with_stdio(false);
	cin>>n>>k;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	dp[0]=0;
	for(int i=0;i<n-1;i++)
	{
		for(int j=1;j<=k;j++)
		{
			dp[i+j]=min(dp[i+j],dp[i]+abs(a[i+j]-a[i]));
		}
	}
	cout<<dp[n-1]<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define INF 100000000

int n, k, h[100001], memo[100001];

int FROG2(int i)
{
	if(i == n)
	return 0;
	if(memo[i] != -1)
	return memo[i];
	int ans = INF;
	for(int j=1;j<=k;j++)
	{
		if(i+j <= n)
		ans = std::min(ans, abs(h[i] - h[i+j]) + FROG2(i+j));
	}
	memo[i] = ans;
	return ans;
}

int main(void)
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);
	std::cout.tie(NULL);
	memset(memo, -1, sizeof(memo));
	std::cin>>n>>k;
	for(int i=1;i<=n;i++)
	std::cin>>h[i];
	int ans = FROG2(1);
	std::cout<<ans<<"\n";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
int mod=int(1e9)+7;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;
    int w;
    cin>>w;
    int d[n+1],v[n+1];
    for(int i=0;i<n;i++){
        cin>>d[i+1];
        cin>>v[i+1];
    }
    ll dp[n+1][w+1];
    for(int i=0;i<=n;i++){
        for(int j=0;j<=w;j++){
            dp[i][j]=0;
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=w;j++){
            if(d[i]<=j){
                dp[i][j]=max(dp[i-1][j],dp[i][j]);
                dp[i][j]=max(dp[i-1][j-d[i]]+v[i],dp[i][j]);
            }
            else{
                dp[i][j]=dp[i-1][j];
            }
        }
    }
    cout<<dp[n][w]<<endl;
}

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.


#include<bits/stdc++.h>
using namespace std;
#define nl cout<<'\n';
#define int long long
#define ff first
#define ss second
#define _lmax 1e14
#define pb push_back
#define all(v) v.begin(),v.end()
void print(int a)                  {              cout<<a<<'\t'<<'\n';                      }
void print(int a,int b)                {          cout<<a<<'\t'<<b<<'\t'<<'\n';             }
void print(int a,int b,int  c)             {      cout<<a<<'\t'<<b<<'\t'<<c<<'\n';          }
void print(int a,int  b,int  c,int  d)         {  cout<<a<<'\t'<<b<<'\t'<<c<<'\t'<<d<<'\n'; }
void print(int a,int  b,int  c,int  d,int  e)     { cout<<a<<'\t'<<b<<'\t'<<c<<'\t'<<d<<'\t'<<e<<'\n';}
void print(vector<int> &a)                        { for(auto i:a)  cout<<i<<" ";   nl;      }
void print(string s)                              { cout<<s;nl;                             }

void solve()
{
    int n,maxWeight;    cin>>n>>maxWeight;
    int v[n] , w[n] , dp[n+1][maxWeight+1]={};

    for(int i=0 ; i<n ; i++)        cin>>w[i]>>v[i];

    dp[0][0] = 0;
    dp[0][w[0]] = v[0];
    int ans = INT_MIN;

    for(int i=1 ; i<n ; i++)
    {
        dp[i][0] = 0;
        for(int j=1 ; j<=maxWeight ; j++)
        {
            // we did not selected this item
            dp[i][j] = dp[i-1][j];

            // picked current object
            if(j>= w[i])
                dp[i][j] = max(
                            dp[i][j] ,
                            dp[i-1][ j - w[i] ] + v[i]
                            );
            ans = max(dp[i][j] , ans);
        }
    }
    print(ans);
}

int32_t main()
{
   // ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
   //  int t;  cin>>t; while(t--)
    solve();
}/*

   vector<int> a = {157,26,9,-5782,0,5,17,-642,318,9,70,9,3210,-39,821,94,72,8,9,53,12,3,23};

*/


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,w;
	cin >> n >> w;
	vector<vector<int> > a(n,vector<int>(2));
	for(int i=0;i<n;i++)
		cin >> a[i][0] >> a[i][1];
	vector<vector<int> > dp(n+1,vector<int>(w+1));
	for(int i=0;i<=n;i++)
		dp[i][0] = 0;
	for(int i=0;i<=w;i++)
		dp[0][i] = 0;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=w;j++)
			dp[i][j] = max(dp[i-1][j],dp[i-1][j-a[i-1][0]] + a[i-1][1]);
	return dp[n][w];
}


#include <iostream>
#include <cstdint>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

const int MAX = 100;
const int W_M = 1e5;
int weight[MAX];
int val[MAX];

pair<bool, long> total_w[W_M];

int main() {

    int n, w, w_max;
    long max_elem = 0;
    w_max = 0;
    cin >> n >> w;

    for(int i = 0; i < n; i++) {
        cin >> weight[i] >> val[i];
        w_max += weight[i];
    }

    for (int i = 0; i < W_M; i++) {
        total_w[i].first = false;
        total_w[i].second = 0;
    }

    //memset(total_w.second, 0, W_M);

    total_w[0].first = true;
    total_w[0].second = 0;
    for (int i = 0; i <= n; i++) {
        for (int j = w_max; j >= 0; j--) {
            if(total_w[j].first) {
                total_w[weight[i] + j].first = true;
                total_w[weight[i] + j].second = total_w[j].second + val[i];
            }
        }
    }

    for (int i = 0; i <= w; i++) {
        if (total_w[i].second > max_elem)
            max_elem = total_w[i].second;
    }

    cout << max_elem;

    // for(int i = 0; i <= w; i++) {
    //     cout << total_w[i].second << " ";
    // }

    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
#define ll long long

int main() {

	ll n, we;
	cin >> n >> we;

	vector<ll> a(n + 1), w(n + 1);

	for (ll i = 1; i < n + 1; i++) {
		cin >> w[i] >> a[i];
	}

	ll dp[n + 1][we + 1];
	for (ll i = 0; i <= we; i++) {
		dp[1][i] = 0;
	}
	dp[1][w[1]] = a[1];

	for (ll i = 2; i <= n; i++) {

		for (ll j = 0; j <= we; j++) {

			dp[i][j] = dp[i - 1][j];
			if (w[i] > j) continue;

			dp[i][j] = max(dp[i][j], a[i] + dp[i - 1][j - w[i]]);

		}
	}

	cout << *max_element(dp[n], dp[n] + we + 1);

	// for (ll i = 0; i <= n; i++) {
	// 	for (ll j = 0; j <= we; j++) {
	// 		cout << dp[i][j] << " ";
	// 	}
	// 	cout << endl;
	// }

}


#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0;i<n;i++)
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0;}

int main() {
    int n,w;
    cin >> n >> w;
    ll weight[110];
    ll value[110];
    vector<vector<ll>>dp(n+10,vector<ll>(w+100));
    rep(i,n) {
        cin >> weight[i] >> value[i];
    }
    rep(i,n)rep(j,w+1) {
        if(j>=weight[i]) chmax(dp[i+1][j],dp[i][j-weight[i]]+value[i]);
        chmax(dp[i+1][j],dp[i][j]);
    }
    cout << dp[n][w] << endl;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
int solve(vector<vector<int>>dp,int idx,int w,vector<pair<int,int>>v)
{
    if(idx==0)
    return 0;
    if(w==0)
    return 0;
    if(dp[idx][w])
    return dp[idx][w];
    if(w>=v[idx].first)
    dp[idx][w]=max(solve(dp,idx-1,w,v),solve(dp,idx-1,w-v[idx].first,v)+v[idx].second);
    else
    dp[idx][w]=solve(dp,idx-1,w,v);
    return dp[idx][w];
}
int32_t main() {
    int n,w;
    cin>>n>>w;
    vector<pair<int,int>>v(n+1);
    for(int i=0;i<n;i++)
    {
        int a;
        int b;
        cin>>a>>b;
        v[i+1]=make_pair(a,b);
    }
    vector<vector<int>>dp(n+1,vector<int>(w+1,0));
    cout<<solve(dp,n,w,v);
}


#include <iostream>
#include <iostream>
#include <string.h>
#include <vector>
#include <queue>
#include <cstdio>
#include <math.h>
#include <map>
#include <algorithm>
#include <deque>
#include <stack>
#include<set>
//#include <bits/stdc++.h>
#define lson l,mid,rt<<1

#define rson mid+1,r,rt<<1|1

const int N=1e5+100;
using namespace std;

typedef long long ll;
const int inf=0x3f3f3f3f;

ll dp[200],a[200];
int main()
{
    int n,W;scanf("%d%d",&n,&W);
    ll w,v;
    for(int i=1;i<=n;i++){
            scanf("%lld%lld",&w,&v);
        for(int j=W;j>=w;j--)
         dp[j]=max(dp[j],dp[j-w]+v);
    }
    printf("%lld\n",dp[W]);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, W;
    cin >> n >> W;

    int w[n], v[n];
    for(int i = 0; i < n; i++) cin >> w[i] >> v[i];

    long long dp[n+1][W+1];
    for(int i = 0; i <= W; i++) {
        dp[0][i] = 0;
    }

    for(int i = 0; i < n; i++) {
        for(int j = 0; j <= W; j++) {
            long long m = dp[i][j];
            if(j - w[i] >= 0) {
                m = max(m, dp[i][j-w[i]] + v[i]);
            }
            dp[i+1][j] = m;
        }
    }

    cout << dp[n][W] << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e2+10;
const int M = 1e5+10;
const int INF = 2e9;
int v[N],w[N];
ll dp[N][M];
int main()
{
	int n, W;
	cin >> n >> W;
	for(int i=1;i<=n;i++)
	{
		cin >> w[i] >> v[i];
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=W;j++)
		{
			if(w[i]<=j)
			{
				dp[i][j]=max(dp[i-1][j],dp[i][j-w[i]]+v[i]);
			}
			else
			{
				dp[i][j]=dp[i-1][j];
			}
		}
	}
	cout<<dp[n][W];
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5, M = 1e2 + 2;

long long n, w, rem[N], cost[N], take, leave;

long long mem[M][N] = {};

int main()
{
	scanf("%lld %lld", &n, &w);
	for(int i = 0 ; i < n; ++i)	scanf("%lld %lld", rem + i, cost + i);

	for(int i = n - 1; i >= 0; --i)
	{
		for(int r = 0; r <= w; ++r)
		{
			if(r + rem[i] > w)
				take = 0;
			else
				take = mem[i + 1][r + rem[i]] + cost[i];

			leave = mem[i + 1][r];
			mem[i][r] = max(take, leave);
		}
	}
			printf("%lld\n", mem[0][0]);
	return 0;
}


#include<iostream>
using namespace std;
long long f[100010],w[110],c[110];
int main()
{
    long long n,v;
    cin>>n>>v;
    for(int i=1;i<=n;i++)
        cin>>c[i]>>w[i];
    for(long long i=1;i<=n;i++)
        for(long long j=v;j>=c[i];j--)
            f[j]=max(f[j],f[j-c[i]]+w[i]);
    cout<<f[v]<<endl;
    return 0;
}


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

using ll=long long;

main(){
    int n,we;
    cin >> n >> we;
    int w[n],v[n];
    for (int i=0;i<n;i++) cin >> w[i] >> v[i];
    ll dp[n+1][we+1] = {{0}};
    for (int i=1;i<n+1;i++){
        for (int i2=1;i2<we+1;i2++){
            if (i2<w[i-1]){
                dp[i][i2]=dp[i-1][i2];
            }
            else{
                dp[i][i2]=max(dp[i-1][i2],dp[i-1][i2-w[i-1]]+v[i-1]);
            }
        }
    }
    cout << dp[n][we] << endl;
}


#include<bits/stdc++.h>
#define ll                 long long
#define ra                 return a;
#define pb                 push_back
#define sti                stack<ll>
#define spi                stack<pair<ll,ll>>
#define S                  second
#define msi                map<string,ll >
#define msi                map<string,ll >
#define mii                map<ll ,ll >
#define dbg(x)             { cerr<<#x<<": "<<x<< endl; }
#define dbg2(x,y)          { cerr<<#x<<": "<<x<<" , "<<#y<<": "<<y<<endl; }
#define dbg3(x,y,z)        { cerr<<#x<<": "<<x<<" , "<<#y<<": "<<y<<" , "<<#z<<": "<<z<<endl; }
#define mis                map<ll , string>
#define rep(i,a,b)         for(ll  i=a;i<b;i++)
#define mpi                map<pair<ll ,ll >,ll >
#define pii                pair<ll ,ll >
#define ppb                pop_back
#define F                  first
#define vi                 vector<ll >
#define vii                vector<pair<ll , ll >>
#define si                 set <ll>
#define vs                 vector<string>
#define all(a)             (a).begin(),(a).end()
#define sz(x)              (ll )x.size()
#define hell               1000000007
#define bs                 binary_search
#define mp                 make_pair
#define qi                 queue<ll>
#define qs                 queue<string>
#define qpi                queue<pair<ll,ll>>
#define qps                queue<pair<ll,string>>
#define endl               '\n'
#define itr(a,it)          for(typeof(a.begin()) it=a.begin();it!=a.end();it++)// This will produce const_iterator for const object and normal iterator for non-const object
#define ss                 set<string>
using namespace std;
#define N  300005

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n,w;
    cin>>n>>w;
    pair<ll,ll> a[n];
    for(ll i=0;i<n;i++)
    {
        ll q,r;
        cin>>q>>r;
        a[i]=make_pair(q,r);
    }
    sort(a,a+n);
    /*for(ll i=0;i<n;i++)
    {
        cout<<a[i].first<<" "<<a[i].second<<endl;
    }*/
    ll table[n][w+1];
    for(ll i=0;i<=w;i++)
    {
        if(i<a[0].first){table[0][i]=0;}
        else{table[0][i]=a[0].second;}
    }
    for(ll i=0;i<n;i++)
    {
        table[i][0]=0;
    }
    ll z=0;
    for(ll i=1;i<n;i++)
    {
        for(ll j=1;j<=w;j++)
        {
            if(j<a[i].first)
            {
                table[i][j]=table[i-1][j];
            }
            else
            {
                  table[i][j]=max(table[i-1][j],a[i].second+table[i-1][j-a[i].first]);

            }
        }
    }
    /*for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<=w;j++)
        {
            cout<<table[i][j]<<" ";
        }
        cout<<endl;
    }*/
    cout<<table[n-1][w];

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const ll mod = (ll)1e9+7;
const int INF = 0x3f3f3f3f;
ll max(ll a, ll b){return a > b ? a : b;}
ll min(ll a, ll b){return a < b ? a : b;}

int mpow(int b, int e)
{
    int ret=1;
    for(int i=0; i<e; i++)
    {
        ret=ret*b%mod;
    }
    return ret;
}

ll dp[100001];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll n, w, w1, val;
    cin >> n >> w;
    for(int i=1; i<=n; i++)
    {
        cin >> w1 >> val;
        for(int j=w-w1; j>=0; j--)
        {
            if(dp[j]) dp[j+w1] = max(dp[j+w1], dp[j]+val);
        }
        dp[w1] = max(dp[w1], val);
    }
    ll ans=0;
    for(int i=1; i<=w; i++) ans = max(ans, dp[i]);
    cout << ans;
}


#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <ctime>
#include <cmath>
using namespace std;

int max(int a, int b) {
	if (a > b) {
		return a;
	}
	return b;
}

int task(int N, int W, vector<int> &weight, vector<int> &value) {
	vector<vector<int>> result(N + 1, vector<int> (W + 1, 0));
	for (int i = 1; i <= N; i++) {
		for (int j = 0; j <= W; j++) {
			if (j >= weight[i-1]) {
				result[i][j] = max(result[i-1][j], value[i-1] + result[i-1][j - weight[i-1]]);
			} else {
				result[i][j] = result[i-1][j];
			}
		}
	}
	return result[N][W];
}

void read_input(int &N, int &W, vector<int> &weight, vector<int> &value) {
	string seq;
	// read N, K
	getline(cin, seq);
	istringstream iss(seq);
	iss >> N;
	iss >> W;
	iss.clear();
	int buf;
	for (int i = 0; i < N; i++) {
		getline(cin, seq);
		iss.str(seq);
		iss >> buf;
		weight.push_back(buf);
		iss >> buf;
		value.push_back(buf);
		iss.clear();
	}
}

int main() {
	int N, W;
	vector<int> weight, value;
	read_input(N, W, weight, value);
	//unsigned init_time = clock();
	cout << task(N, W, weight, value) << endl;
	//unsigned end_time = clock();
	//cout << double(end_time - init_time) / CLOCKS_PER_SEC << " seconds" << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N, W;
int w[101], v[101];
ll dp[101][100001];
ll shop(int i, int wi) {
	if(dp[i][wi] != -1)
		return dp[i][wi];
	if(w[i] > wi || i == N)
		return 0;
	dp[i][wi] = max(dp[i][wi], v[i] + shop(i+1, wi - w[i]));
	dp[i][wi] = max(dp[i][wi], shop(i+1, wi));
	return dp[i][wi];
}
int main() {
	cin >> N >> W;
	for(int i=0;i<N;i++)
		cin >> w[i] >> v[i];
	for(int i=0;i<101;i++)
		for(int j=0;j<100001;j++)
			dp[i][j] = -1;
	cout << shop(0,W) << "\n";
	return 0;
}


#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using Int = long long;
Int inf = 1000000000000000001LL;
int main() {
    int N, W;
    cin >> N >> W;

    vector<vector<Int>> dp = vector<vector<Int>>(N+10,vector<Int>(W+10,0));
    vector<Int> weights = vector<Int>(N);
    vector<Int> values = vector<Int>(N);

    for(int i=0; i<N; i++){
        cin >> weights[i] >> values[i];
    }

    for(int i =0;i<W; i++){
        dp[0][W] = 0;
    }

    for(int i = 0; i<N; ++i){
        for(int w=0; w<=W; ++w){
            if(w >= weights[i]){
                dp[i+1][w] =  max(
                        dp[i][w-weights[i]]+values[i],
                        dp[i][w]
                );
            }else{
                dp[i+1][w] = dp[i][w];

            }

        }
    }

    cout <<  dp[N][W] << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long
ll n,W,w[100],v[100],dp[101][100001];
ll rec(ll i,ll j){
  if(dp[i][j]>=0) return dp[i][j];
  ll res;
  if(i==n) res=0;
  else if(j<w[i]) res=rec(i+1,j);
  else res=max(rec(i+1,j),rec(i+1,j-w[i])+v[i]);
  return dp[i][j]=res;
}
int main(){
  cin>>n>>W;
  rep(i,n) cin>>w[i]>>v[i];
  memset(dp,-1,sizeof(dp));
  cout<<rec(0,W);
}


#include <bits/stdc++.h>

#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define ll long long
#define INF 999999999999
#define MOD 1000000007

using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

int iy[]={0, 0, 1, -1};
int ix[]={1, -1, 0, 0};

ll n, k, w[110], v[110], ans, dp[110][100010];

int main(){
	cin >> n >> k;
	for(int i=1;i<=n;i++){
		cin >> w[i] >> v[i];
	}
	for(int i=0;i<=n;i++){
		for(int j=0;j<=k;j++){
			if(i!=0||j!=0) dp[i][j] = -1;
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=0;j<=k;j++){
			if(j-w[i]>=0&&dp[i-1][j-w[i]]!=-1) dp[i][j] = max(dp[i][j], dp[i-1][j-w[i]] + v[i]);
			if(dp[i-1][j]!=-1) dp[i][j] = max(dp[i][j], dp[i-1][j]);
			if(i==n) ans = max(ans, dp[i][j]);
		}
	}
	cout << ans << endl;
	return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <typeinfo>
#include <numeric>
#include <functional>
#include <unordered_map>
#include <bitset>
#include <stack>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for(ll i = 0; i < n; i++)

int main() {
    ll n, W;
    cin >> n >> W;
    vector<ll> w(n), v(n);
    REP(i, n){
        cin >> w[i] >> v[i];
    }

    vector<vector<ll>> dp(n + 1, vector<ll>(W + 1));
    REP(i, n){
        REP(j, W + 1){
            if(j + w[i] <= W){
                dp[i + 1][j + w[i]] = max(dp[i + 1][j + w[i]], dp[i][j] + v[i]);
            }
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        }
    }
    cout << dp[n][W] << endl;
}


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

#define fast ios::sync_with_stdio(0); cin.tie(NULL);
#define fi first
#define se second
#define mp make_pair
#define pi 3.14159265359
#define MOD 1000000007
#define inf 1e18
#define setbits(x) __builtin_popcountll(x)
typedef long long ll;
#define mp make_pair
#define ALL(v) v.begin(), v.end()
#define F(i,s,n) for(i=s;i<n;++i)
#define pb push_back
#define eb emplace_back
#define tin freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define pll pair<ll,ll>
ll max(ll a,ll b)
{
    return a*(a>=b)+b*(b>a);
}
string smax(string a,string b)
{
    if(a.size()>b.size())return a;
    return b;
}
/*----------------------------------------------------------*/
string a,b;

void solve()
{
    cin>>a>>b;
    ll n=a.size(),m=b.size();
    ll dp[n+1][m+1];
    for(ll i=0;i<n+1;++i)dp[i][0]=0;
    for(ll i=0;i<m+1;++i)dp[0][i]=0;
    for(ll i=1;i<n+1;++i)
    {
        for(ll j=1;j<m+1;++j)
        {
            if(a[i-1]==b[j-1])  dp[i][j]=1+dp[i-1][j-1];
            else
                dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
        }
    }//cout<<dp[n][m];
    if(dp[n][m]==0){cout<<" ";return;}
    ll i=n,j=m;
    string s="";
    while(i!=0&&j!=0)
    {
        if(dp[i][j]==1+dp[i-1][j-1])
        {
            --i;--j;s+=a[i];
            //<<" "<<s<<endl;
        }
        else if(dp[i][j]==dp[i-1][j])--i;
        else --j;
    }
    for(ll i=s.size()-1;i>=0;--i)
    {
        cout<<s[i];
    }
}

int main()
{
    //tin
    fast
    ll tc=1;//cin>>tc;
   for(ll i=1;i<=tc;++i)
   {
    //cout<<"Case #"<<i<<": ";
    solve();
    if(i!=tc)cout<<"\n";
   }
return 0;
}
//End-Cases
//Precomputation
//Segmentation Fault
//Complexity
//Break it up
//Sub Problems

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


#include <bits/stdc++.h>

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define dump_1(x1) cerr << #x1 << ": " << x1 << endl
#define dump_2(x1, x2) \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define dump_3(x1, x2, x3)                                                \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
       << x3 << endl
#define dump_4(x1, x2, x3, x4)                                            \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
       << x3 << ", " #x4 << ": " << x4 << endl
#define dump_5(x1, x2, x3, x4, x5)                                        \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#define dump(...) \
  CHOOSE((__VA_ARGS__, dump_5, dump_4, dump_3, dump_2, dump_1, ~))(__VA_ARGS__)
#define check(s) cerr << s << endl

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

using namespace std;

using ll = long long;

vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

const ll LINF = 2e18;
const int INF = 1e9;

void solve(std::string s, std::string t) {
  int N = sz(s);
  int M = sz(t);
  vector<vector<int>> dp(N + 1, vector<int>(M + 1, 0));

  rep(i, N) {
    rep(j, M) {
      int p = dp.at(i + 1).at(j);
      int q = dp.at(i).at(j + 1);
      if (p > q) {
        dp.at(i + 1).at(j + 1) = p;
      } else {
        dp.at(i + 1).at(j + 1) = q;
        if (s.at(i) == t.at(j)) {
          dp.at(i + 1).at(j + 1)++;
        }
      }
    }
  }

  string ans = "";
  int l = dp.at(N).at(M);
  int i = N - 1;
  int j = M - 1;
  while (l > 0) {
    if (s.at(i) == t.at(j)) {
      ans += s.at(i);
      i--;
      j--;
      l--;
    } else if (dp.at(i + 1).at(j + 1) == dp.at(i).at(j + 1) && i > 0) {
      i--;
    } else if (j > 0) {
      j--;
    }
  }
  reverse(all(ans));
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  std::string s;
  std::cin >> s;
  std::string t;
  std::cin >> t;
  solve(s, t);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	string s,x;
	cin>>s>>x;
	string dp[s.length()+1][x.length()+1];
	for (int i=0;i<=s.length();i++){
		for(int j=0;j<=x.length();j++){
			if (i==0 || j==0){
				dp[i][j]="";
			}
			else if(s[i-1]==x[j-1]){
				dp[i][j]=dp[i-1][j-1]+s[i-1];

			}
			else{
				if(dp[i-1][j].length()>dp[i][j-1].length()){
					dp[i][j]=dp[i-1][j];
				}
				else{
					dp[i][j]=dp[i][j-1];
				}
			}
		}
	}
	cout<<dp[s.length()][x.length()];

}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)

template<class T> inline bool chmax(T &a, const T &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T> inline bool chmin(T &a, const T &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}init_;

#ifdef DEBUG
template <class T, class N> void verr(const T& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
ll dp[3004][3004];

int main() {
    string S, T;
    cin >> S >> T;
    ll sn = S.size(), tn = T.size();

    rep(i, sn){
        rep(j, tn){
            if (S[i] == T[j]) chmax(dp[i + 1][j + 1], dp[i][j] + 1);
            chmax(dp[i + 1][j + 1], max(dp[i + 1][j], dp[i][j + 1]));
        }
    }

    string ans;

    ll ti = tn - 1, si = sn - 1;
    while(ti >= 0 and si >= 0){
        while (ti >= 0 and dp[si + 1][ti + 1] == dp[si + 1][ti]) ti--;
        if (ti < 0) break;
        while (si >= 0 and dp[si + 1][ti + 1] == dp[si][ti + 1]) si--;
        if (si < 0) break;

        ans += S[si];
        ti--, si--;
    }

    reverse(all(ans));
    cout << ans << endl;
}


# include <bits/stdc++.h>
# define sz(x) (int)((x).size())
# define int long long
# define F first
# define S second
# define pb push_back
# define all(x) x.begin(), x.end()
# define pqueue priority_queue
# define mset multiset
# define umap unordered_map
# define Speed() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
# define rep(i,n) for(int i=0;i<n;++i)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 3005;

string s,t,ans;
int used[N],dp[N][N];
char ret[N];

void solve(){
	cin>>s>>t;
	s="#"+s;
	t="#"+t;
	for(int i=1;i<=sz(s);++i){
		for(int j=1;j<=sz(t);++j){
			if(s[i]==t[j]){
				dp[i][j]=dp[i-1][j-1]+1;
				if(!used[dp[i][j]]){
					used[dp[i][j]]=1;
					ret[dp[i][j]]=s[i];
				}
			}
			else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
		}
	}
	for(int i=1;i<=dp[sz(s)][sz(t)];++i){
		ans+=ret[i];
	}
	cout<<ans;
}

signed main(){
	Speed();
	//int T;cin>>T;while(T--)
	solve();
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
string s,t;
ll solve(int i,int j,string &ans){
  if(i>=s.size()||j>=t.size()){
    ans+='\0';
    return 0;
  }
  ll l=0;
  if(s[i]==t[j]){
   ans+=s[i];
   l=1+solve(i+1,j+1,ans);
  }
  else{
    l=max(solve(i,j+1,ans),solve(i+1,j,ans));
  }
  return l;
}

int main(){

cin>>s>>t;
string ans="";
ll a=solve(0,0,ans);
cout<<ans;
return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <climits>
using namespace std;
vector<vector<pair<int, string>>> dp(3001,vector<pair<int,string>>(3001,{-1,""}));
string x,y;
// Find a longest Subsequence b/w 2 strings using memoization
pair<int, string> LCSstring(int n,int m){
    if(n==0 || m==0){

        return make_pair(0, "");
    }
    if(dp[n][m].first != -1){
        return dp[n][m];
    }
    cout<<n<<" "<<m<<endl;
    if(x[n-1] == y[m-1]){
        auto p = LCSstring( n-1, m-1);
        p.first +=1;
        p.second += x[n-1];
        dp[n][m] = p;
        return dp[n][m];
    }
    else{
        auto p1 = LCSstring( n-1, m);
        auto p2 = LCSstring(n, m-1);

        if(p1.first >= p2.first){
            dp[n][m] = p1;
            return dp[n][m];
        }
        else{
            dp[n][m] = p2;
            return dp[n][m];
        }
    }
    return dp[n-1][m-1];
}

int main() {
    string x, y;
    cin >> x >> y;
    int a=x.size();
    int b=y.size();
    auto p = LCSstring(a, b);
    cout << p.second<< endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1e9+1
string lcs(string s1,string s2){

    int n=s1.length();
    int m=s2.length();

    vector<vector<int>> dp(n+1,vector<int> (m+1,0));

    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){

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

            }else{
                dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
            }
        }
    }

    int i=n,j=m;

    string ans="";

    while(i>=0 && j>=0){

        if(s1[i-1]==s2[j-1]){
            ans+=s1[i-1];
            i--;
            j--;

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

            }else{
                j--;

            }
        }

    }

    reverse(ans.begin(),ans.end());

    return ans;

}

int main() {
    string s1,s2;
    cin>>s1>>s2;

    cout<<lcs(s1,s2);

}


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

#define pb push_back
#define F first
#define S second

#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define int long long
#define ll long long

const int N = 3e3 + 5;
int dp[N][N];
string s1, s2, ans;

int rec(int x, int y){
	if(x == s1.size() || y == s2.size())
		return 0;
	if(dp[x][y] != -1)
		return dp[x][y];
	if(s1[x] == s2[y])
		return dp[x][y] = 1 + rec(x + 1, y + 1);
	return dp[x][y] = max(rec(x + 1, y), rec(x, y + 1));
}

void solve(){
	memset(dp, -1, sizeof dp);
	cin >> s1 >> s2;
	rec(0, 0);
	int first = 0, second = 0;
	while(first < s1.size() && second < s2.size()){
		if(s1[first] == s2[second]){
			ans += s1[first];
			first++;
			second++;
		}
		else
		if(dp[first][second + 1] >= dp[first + 1][second]){
			second++;
		}
		else
			first++;

	}
	cout << ans << "\n";
}

signed main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t = 1;
	//cin >> t;
	while(t--)
		solve();
	return 0;
}


#include<bits/stdc++.h>
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define all(x) (x).begin(),(x).end()
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long int ll;
typedef long double ld;
const ll INF=(1LL<<62);
const ld pi=acosl((ld)-1);
const ll mod = 1000000007;
// const ll mod = 1234567;
const int dx[4]={0,1,0,-1};
const int dy[4]={1,0,-1,0};
const int ddx[8]={1,0,-1,-1,-1,0,1,1};
const int ddy[8]={1,1,1,0,-1,-1,-1,0};
#define endn "\n"
#define TO_STRING(VariableName) # VariableName

template <typename T>ostream &operator<<(ostream &out,const vector<T> &v) {rep(i,(int)v.size()-1)cout<<v[i]<<" ";cout<<v[(int)v.size()-1];return out;}
template <typename T1, typename T2>ostream &operator<<(ostream &out, const map<T1, T2> &p) {out << "(" << p.first << ", " << p.second << ")";return out;}
template <typename T1, typename T2>ostream &operator<<(ostream &out, const pair<T1, T2> &p){out << "(" << p.first << ", " << p.second << ")";return out;}
template<class T>void debag(const T &obj){cout<<obj<<endl;}

bool solve(){
  //dp[i][j]...s[0,i)とt[0,j)の最長部分列
  string s,t;cin>>s>>t;
  ll n=s.size();
  ll m=t.size();
  vector<vector<ll>>dp(n+1,vector<ll>(m+1,0));
  rep(i,n+1)rep(j,m+1)dp[i][j]=0;
  vector<vector<pair<ll,ll>>>from(n+1,vector<pair<ll,ll>>(m+1,pair<ll,ll>(-1,-1)));

  // cout<<"dp"<<endl;
  // rep(i,n+1)cout<<dp[i]<<endl;

  rep(i,n)rep(j,m){
    if(chmax(dp[i+1][j],dp[i][j]))from[i+1][j]=pair<ll,ll>(i,j);
    if(chmax(dp[i][j+1],dp[i][j]))from[i][j+1]=pair<ll,ll>(i,j);
    if(chmax(dp[i+1][j+1],dp[i][j]))from[i+1][j+1]=pair<ll,ll>(i,j);
    if(s[i]==t[j]){
      if(chmax(dp[i+1][j+1],dp[i][j]+1)){
        from[i+1][j+1]=pair<ll,ll>(i,j);
      }
    }
  }

  // cout<<dp[n][m]<<endl;
  // cout<<"dp"<<endl;
  // rep(i,n+1)cout<<dp[i]<<endl;

  vector<pair<ll,ll>>v;
  ll x=n,y=m;
  while(x!=-1 and y!=-1){
    // cout<<from[x][y]<<endl;
    v.push_back(from[x][y]);
    auto p=from[x][y];
    x=p.first;
    y=p.second;
  }

  reverse(all(v));
  string ans;
  for(auto p:v){
    // cout<<p<<endl;
    if(p.first==-1 or p.second==-1)continue;
    if(s[p.first]==t[p.second])ans.push_back(s[p.first]);
  }
  cout<<ans<<endl;

  return false;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout<<fixed<<setprecision(30);
    solve();
}


// first second push_back unordered return continue break vector visited check flag bool while iterator begin end lower_bound upper_bound temp true false INT_MAX INT_MIN insert erase clear pop push compare INT64_MAX INT64_MIN  reverse replace stringstream string::npos length() substr front
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()
const int mod = 1e9 + 7 ;
string ans ="";
struct hash_pair {template <class T1, class T2>size_t operator()(const pair<T1, T2>& p)const{auto hash1=hash<T1>{}(p.first);auto hash2=hash<T2>{}(p.second);return hash1^hash2;}};
void add_self(int& a , int b){a =  (a + b)%mod;}
//#define (x)  get<0>(x)
//#define (x)  get<1>(x)
//#define (x)  get<2>(x)
unordered_set<pair<int,int>,hash_pair> memo ;
void lcs(string& s ,string& t , int i , int j , string k){
	// cout<<i<<" "<<j<<" "<<k<<endl;
if(memo.find({i,j})!=memo.end()) return;
memo.insert({i,j});
	if(i==s.length()||j==t.length()){
		if(k.length()>=ans.length())
			ans = k;
return;
	}

	if(s[i]==t[j]){
		k = k + s[i];
		lcs(s,t,i+1,j+1,k);
		return;
	}

	lcs(s,t,i+1,j,k);
	lcs(s,t,i,j+1,k);

}

signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

string s , t ;
cin>>s>>t;
lcs(s,t,0,0,"");

cout<<ans;

}


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll N=5005;
int main(){
	ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
	ll i,j,dp[N][N],n,m;
	string ans="",a,b;
	fflush(stdin); cin>>a; n=a.size();
	fflush(stdin); cin>>b; m=b.size();
	for(i=0;i<n;++i){
		if(a[i]==b[0]){
			for(j=i;j<n;++j) dp[j][0]=1;
			break;
		}
		else dp[i][0]=0;
	}
	for(i=0;i<m;++i){
		if(b[i]==a[0]){
			for(j=i;j<m;++j) dp[0][j]=1;
			break;
		}
		else dp[0][i]=0;
	}
	for(i=1;i<n;++i){
		for(j=1;j<m;++j){
			if(a[i]==b[j]) dp[i][j]=dp[i-1][j-1]+1;
			else dp[i][j]=min(dp[i-1][j],dp[i][j-1]);
		}
	}
//	for(i=0;i<n;++i){
//		for(j=0;j<m;++j) cout<<dp[i][j]<<" ";
//		cout<<endl;
//	}
	i=n-1; j=m-1;
	while((i>=0)&&(j>=0)){
		if(a[i]==b[j]){
			ans+=a[i];
			--i; --j;
			continue;
		}
		if(i==0){
			--j;
			continue;
		}
		if(j==0){
			--i;
			continue;
		}
		if(dp[i-1][j]>dp[i][j-1]) --i;
		else --j;
	}
	reverse(ans.begin(),ans.end());
	cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;
typedef  long long ll;
int mod = 1e9+7;

int main(){

	ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    string s1, s2;
    cin>>s1>>s2;

    int n = s1.length();
    int m = s2.length();

    vector<vector<pair<ll, pair<ll, ll>>>> dp(n+1, vector<pair<ll, pair<ll, ll>>>(m+1, {0, {0, 0}}));

    for(ll i=0; i<=n; i++){
    	for(ll j=0; j<=m; j++){
    		if(i && j){

    			if(s1[i-1]==s2[j-1]){
    				dp[i][j] = {dp[i-1][j-1].first+1, {i-1, j-1}};
    			}else{
    				if(dp[i-1][j].first>dp[i][j-1].first){
    					dp[i][j] = {dp[i-1][j].first, {i-1, j}};
    				}else dp[i][j] = {dp[i][j-1].first, {i, j-1}};
    			}
    		}
    	}
    }
    string ans;
    ll i=n, j=m;
    // cout<<dp[i][j].first<<endl;
    while(i && j){
    	// cout<<dp[i][j].second.first<<" "<<dp[i][j].second.second<<endl;
    	if(dp[i][j].second.first==i-1 && dp[i][j].second.second==j-1){
    		ans+=s2[j-1];
    		i--;
    		j--;
    	}else if(dp[i][j].second.first==i-1){
    		i--;
    	}else{
    		j--;
    	}
    }

    reverse(ans.begin(), ans.end());
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
#define fin ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ii pair<int,int>
#define F first
#define S second
#define pb push_back
#define pf push_front
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define fore(i,a,b) for(int i = a;i < b; i+= 1)
#define forr(i,a) for(int i = a; i >= 0; i--)
#define fori(i,m) for(auto i = m.begin(); i != m.end(); i++)
#define w(t) while(t--)
#define sz(s) int(s.size())
#define cls(a,car) memset(a,car,sizeof (a))
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ii> vii;
const int N = 2 * 1e5 + 5;
const ll mod = 1e9 + 7;
const int INF = INT_MAX;
const ll INFCAD  = ll(INT_MAX) * 2 + 1;
const double E = 1e-9;
// fflush(stdout)
// cout << flush
string a,b;
int dp[3005][3005];
int LCS(int i,int j){
    if(i == sz(a) or j == sz(b))
        return 0;
    if(dp[i][j] != -1)return dp[i][j];
    int &ans = dp[i][j];
    if(a[i] == b[j])
        return ans = LCS(i + 1,j + 1) + 1;
    return ans = max(LCS(i + 1, j), LCS(i, j + 1));

}
int main(){
    //freopen("in","r",stdin);
    //freopen("out","w",stdout);
    fin;
    cls(dp, -1);
    cin >> a >> b;
    dp[sz(a)][sz(b)] = 0;
    for(int i = sz(a) - 1; i >= 0; i--)
        for(int j = sz(b) - 1; j >= 0; j--){
            if(a[i] == b[j])
                dp[i][j] = dp[i + 1][j + 1] + 1;
            else
            dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]);
        }

    //reconstruir
    int i = 0, j = 0;
    string ans = "";
    while(i < sz(a) and j < sz(b)){
        if(a[i] == b[j]){
            ans += a[i];
            i++;
            j++;
        }else
        if(dp[i + 1][j] > dp[i][j + 1])
            i++;
        else
            j++;
    }
    cout << ans << '\n';
    return 0;
}


//
// Created by Hideaki Imamura on 2020-03-15.
//
# include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair <ll, ll> l_l;
typedef pair<int, int> i_i;

template<class T>
inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

# define EPS (1e-7)
# define INF (1e9)
# define PI (acos(-1))
//const ll mod = 1000000007;

string s, t;

int main() {
    cin >> s >> t;

    int n = s.length();
    int m = t.length();
    vector<vector<pair<int, string>>> dp(n + 1, vector<pair<int, string>>(m + 1, make_pair(0, "")));
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (s[i-1] == t[j-1]) {
                dp[i][j] = make_pair(dp[i-1][j-1].first + 1, dp[i-1][j-1].second + s[i-1]);
            } else {
                dp[i][j] = dp[i-1][j].first >= dp[i][j-1].first ? dp[i-1][j] : dp[i][j-1];
            }
        }
    }
    cout << dp[n][m].second << endl;
    return 0;
}


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

int main() {
  int dp[1010][1010];
  string S,T;
  cin >> S;
  cin >> T;
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < S.size(); i++) {
    for (int j = 0; j < T.size(); j++) {
      if (S[i] == T[j]) dp[i + 1][j + 1] = dp[i][j] + 1;
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]);
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]);
    }
  }
  string str = "";
  int i = S.size();
  int j = T.size();
  while (0 < i && 0 < j) {
    int x = dp[i][j];
    while (0 < i && dp[i - 1][j] == x) i--;
    while (0 < j && dp[i][j -1] == x) j--;
    str += S[i - 1];
    i--;
    j--;
  }
  reverse(str.begin(), str.end());
  cout << str << endl;
  return 0;
}


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

const int maxn=3010;
char a[maxn],b[maxn];
int dp[maxn][maxn];
int l1,l2;

void rek(int i, int j)
{
	if(i==0 || j==0)
		return;
	if(dp[i-1][j-1]+1==dp[i][j] && a[i]==b[j])
		{ rek(i-1,j-1); printf("%c",a[i]); }
	else
		{ (dp[i-1][j]>dp[i][j-1]) ? rek(i-1,j) : rek(i,j-1);  }
}

int main()
{
	scanf("%s",a+1);
	scanf("%s",b+1);
	l1=strlen(a+1); l2=strlen(b+1);
	for(int i=1;i<=l1;i++)
	{
		for(int j=1;j<=l2;j++)
		{
			dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
			if(a[i]==b[j])
				dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1);
		}
	}
	rek(l1,l2);
}


// F - LCS (Longest Common Subsequence)

// 最長共通部分文字列の長さだけでなく、部分文字列そのものを求める

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const int MOD = 1e9 + 7;

int dp[3001][3001]; // dp[i][j]: sのi文字目, tのj文字目（1始まり）までのLCS
string s, t;

void update(int i, int j) {
	if (s[i] == t[j]) {
		dp[i][j] = dp[i-1][j-1] + 1;
	} else {
		dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
	}
}

int main(){
	cin >> s >> t;
	int m = s.size();
	int n = t.size();

	s = " " + s;
	t = " " + t;

	for (int i=1; i<=m; i++) {
		for (int j=1; j<=n; j++) {
			update(i, j);
		}
	}

	// 復元
	string ans = "";
	int i = m;
	int j = n;
	while(i>0 && j>0) {
		if (dp[i][j] == dp[i-1][j]) { // dp[i-1][j]からの遷移
			i--;
		} else if (dp[i][j] == dp[i][j-1]) { // dp[i][j-1]からの遷移
			j--;
		} else { // dp[i-1][j-1]からの遷移
			ans = s[i] + ans;
			i--;
			j--;
		}
	}

	cout << ans << endl;

	return 0;
}


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

#define SKILLER ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long

const int N=1e3+5;
string s, t;
int n, m, totW;
int w[105], v[105];
int cache[N][N];

int dp(int i, int j)
{
  if(i == n)return 0;
  if(j == m)return 0;
  if(cache[i][j] != -1)return cache[i][j];
  if(s[i] == t[j]){
    cache[i][j] = dp(i+1, j+1) + 1 ;
    return cache[i][j];
  }
  cache[i][j] = max(dp(i+1, j), dp(i, j+1));
  return cache[i][j] ;
}
void path(int i, int j){
    if(i == n)return;
    if(j == m)return;
    if(s[i] == t[j]){
        cout<<s[i];
        return path(i+1, j+1);
    }
    else if(cache[i][j] == cache[i+1][j])
         return path(i+1, j);
    else return path(i , j+1);
}
int32_t main()
{
    SKILLER

    memset(cache, -1, sizeof(cache));
    cin >> s >>  t;
    n = s.length();
    m = t.length();
    int ans = dp(0,0);
   // cout<<ans<<"\n";
    path(0, 0);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
/*
#ifndef ONLINE_JUDGE
    #define cin f
    #define cout g
    ifstream cin("a.in");
    ofstream cout("a.out");
#endif
*/
int k;
char a[3002],b[3002],S[3002],D[3002][3002];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>(a+1)>>(b+1);
    int N=strlen(a+1);
    int M=strlen(b+1);
    for(int i=1;i<=N;++i)
        for(int j=1;j<=M;++j) D[i][j]=(a[i]==b[j]?1+D[i-1][j-1]:max(D[i-1][j],D[i][j-1]));
    for(int i=N,j=M;i;)
        if(a[i]==b[j]) S[++k]=a[i],--i,--j;
        else D[i-1][j]<D[i][j-1]?--j:--i;
    for(int i=k;i;--i) cout<<S[i];
    return 0;
}


#include <bits/stdc++.h>

#define filein freopen ("in.txt", "r", stdin)
#define fileout freopen ("out.txt", "w", stdout)
#define dbg(x) cerr << #x << ": " << x << endl

using namespace std;
typedef long long ll;

const int maxn = 3005;

char s[maxn], t[maxn];
int slen, tlen;
ll dp[maxn][maxn];
string ans;

int main(){
	scanf ("%s%s", s, t);
	slen = strlen (s);
	tlen = strlen (t);
	for (int i = 1; i <= slen; i++){
		for (int j = 1; j <= tlen; j++){
			dp[i][j] = max (dp[i - 1][j], dp[i][j - 1]);
			if (s[i - 1] == t[j - 1])
				dp[i][j] = max (dp[i][j], 1 + dp[i - 1][j - 1]);
		}
	}
	int i = slen, j = tlen;
	while (dp[i][j]){
		if (dp[i][j] == 1 + dp[i - 1][j - 1]){
			ans += s[i - 1];
			i--;
			j--;
		}
		else if (dp[i][j] == dp[i - 1][j])
			i--;
		else
			j--;
	}
	for (int i = (int) ans.size() - 1; i >= 0; i--)
		cout << ans[i];
	cout << '\n';
	return 0;
}


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

#define endl '\n'

typedef long long ll;
typedef pair<int, int> pii;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	string s, t; cin >> s >> t;

	vector<vector<int>> lcs(s.length() + 1, vector<int>(t.length() + 1));
	vector<vector<int>> dir(s.length() + 1, vector<int>(t.length() + 1));

	for(int i = 1; i <= s.length(); ++i)
		for(int j = 1; j <= t.length(); ++j)
		{
			if(s[i - 1] == t[j - 1]) lcs[i][j] = lcs[i - 1][j - 1] + 1, dir[i][j] = 1;
			else if(lcs[i - 1][j] > lcs[i][j - 1]) lcs[i][j] = lcs[i - 1][j], dir[i][j] = 2;
			else lcs[i][j] = lcs[i][j - 1], dir[i][j] = 3;
		}

	string ans = "";
	int x = s.length(), y = t.length();
	while(x > 0 && y > 0)
	{
		switch(dir[x][y])
		{
			case 1:
				--x, --y, ans += s[x];
				break;
			case 2:
				--x;
				break;
			case 3:
				--y;
				break;
		}
	}

	reverse(ans.begin(), ans.end());
	cout << ans << endl;

	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#define _USE_MATH_DEFINES
#include<math.h>
#include<queue>
#include<deque>
#include<stack>
#include<cstdio>
#include<utility>
#include<set>
#include<list>
#include<cmath>
#include<stdio.h>
#include<string.h>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const ll dx[4] = { 0,1,0,-1 };
const ll dy[4] = { -1, 0, 1,0 };
const int INF = 1e9 + 7;
int gcd(int x, int y) {
	if (x < y)swap(x, y);
	if (y == 0)return x;
	return gcd(y, x%y);
}
void mul(ll a, ll b) {
	a = a * b % INF;
}
double mysqrt(double x) {
	double l = 0, r = x;
	for (int i = 0; i < 64; ++i) {
		double m = (l + r) / 2.0;
		if (m*m < x)l = m;
		else r = m;
	}
	return l;
}
///////////////////////////////////////

int dp[3010][3010];
//dp[i][j]でsのi文字目までtのj文字目まで見た時に
int main() {
	//まず最長の物の長さを求める
	string s, t; cin >> s >> t;
	memset(dp, 0, sizeof(dp));
	for (int i = 0; i < s.size(); ++i) {
		for (int j = 0; j < t.size(); ++j) {
			if (s[i] == t[j]) {
				dp[i + 1][j + 1] = dp[i][j] + 1;
			}
			else {
				dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
			}
		}
	}
	string ans = "";
	int i = s.size(), j = t.size();
	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 = s[i - 1] + ans;
			--i, --j;
		}
	}
	cout << ans << endl;
	return 0;
}


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

#define endl "\n"

typedef long long ll;
typedef pair <ll, ll> pii;
typedef long double ld;

const ll DIM = 3007;
const ll INF = 1000000007;
ll n, m, k;
ll used[DIM][DIM];
// LCL NOT DONE

string s,s1;
int main()
{
   // ios_base::sync_with_stdio(0);
   // cin.tie(0);
   // cout.tie(0);
    cin >> s >> s1;
    for(ll i=1; i<=s.size(); i++)
        for(ll j=1; j<=s1.size(); j++)
        {
            if(s[i-1]==s1[j-1])
                used[i][j]=used[i-1][j-1]+1;
            else
            {
                used[i][j]=max(used[i][j-1],used[i-1][j]);
            }
        }
    ll a=s.size(),b=s1.size();
    string ha="";

    while(a!=0||b!=0)
    {
        if(a==0||b==0)break;
        if(s[a-1]==s1[b-1])
        {
            a--;
            b--;
            ha+=s[a];
        }
        else if(used[a-1][b]==used[a][b])
            a--;
        else
            b--;
    }
    for(ll i=ha.size()-1; i>=0; i--)
        cout<<ha[i];
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s1,s2;
	cin>>s1>>s2;
	int n = s1.length();
	int m = s2.length();
	int dp[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(s1[i-1] == s2[j-1])
			{
				dp[i][j] = dp[i-1][j-1] + 1;
			}
			else
			{
				dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
			}
		}
	}
	string res = "";
	int ind = dp[n][m];
	int i=n,j=m;
	while(i>0 && j>0)
	{
		if(s1[i-1] == s2[j-1])
		{
			res += s1[i-1];
			i--;
			j--;
			ind--;
		}
		else if(dp[i-1][j]>dp[i][j-1])
		{
			i--;
		}
		else
		{
			j--;
		}
	}
	reverse(res.begin(),res.end());
	if(res.size()==0)
	{
		cout<<" ";
		return 0;
	}
	cout<<res<<"\n";
	return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stack>
#include <string>
#include <ctime>
#include <iomanip>
#include <set>
#include <iterator>
#include <map>
using namespace std;

#define pb push_back

#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define for1(i, n) for(int i = 1; i <= (int)(n); i++)

using ll = long long;

typedef vector<int> vi;

const int INF = 1e9 + 5;

void max_self(ll& a, ll b)
{
	a = max(a, b);
}

void max_self(int& a, int b)
{
	a = max(a, b);
}

int main()
{
	string a, b;
	cin >> a >> b;
	vector<vector<int>> dp(a.length() + 1, vector<int>(b.length() + 1, 0));
	for(int i = 0; i < a.length(); i++)
		for (int j = 0; j < b.length(); j++)
		{
			if (a[i] == b[j])
				max_self(dp[i + 1][j + 1], dp[i][j] + 1);
			max_self(dp[i + 1][j], dp[i][j]);
			max_self(dp[i][j + 1], dp[i][j]);
		}
	int size = 0;
	for (auto row : dp)
		for (auto v : row)
			max_self(size, v);
	vector<char> ans(size);
	if (size != 0)
	{
		int i = a.length(), j = b.length();
		while (i > 0 && j > 0)
		{
			if (a[i - 1] == b[j - 1])
			{
				ans[size - 1] = a[i - 1];
				i--, j--, size--;
			}
			else if (dp[i - 1][j] > dp[i][j - 1])
				i--;
			else j--;
		}
		for (auto i : ans)
			cout << i;
	}
	else cout << " " << endl;
	return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
#define M 1000000007
using namespace std;
int main()
{
    ios::sync_with_stdio(0);cin.tie(0);
    string s;
    string t;
    cin>>s>>t;
    int sl=s.length();
    int tl=t.length();
    string ans[sl][tl];
    for(int i=0;i<sl;i++)
    {
        if(s[i]==t[0])
        {
            ans[i][0]=t[0];
            for(int j=i+1;j<sl;j++)
            {
                ans[j][0]=t[0];
            }
            break;
        }
        else
        {
            ans[i][0]="";
        }

    }
    for(int i=0;i<tl;i++)
    {
        if(t[i]==s[0])
        {
            ans[0][i]=s[0];
            for(int j=i+1;j<tl;j++)
            {
                ans[0][j]=s[0];
            }
            break;
        }
        else
        {
            ans[0][i]="";
        }

    }
    for(int i=1;i<sl;i++)
    {
        for(int j=0;j<tl;j++)
        {
            if(s[i]==t[j])
            {
                ans[i][j]=ans[i-1][j-1]+s[i];
            }
            else
            {
                if(ans[i][j-1].length()>ans[i-1][j].length())
                {
                    ans[i][j]=ans[i][j-1];
                }
                else
                {
                    ans[i][j]=ans[i-1][j];
                }

            }

        }
    }
    cout<<ans[sl-1][tl-1];

}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define ran 3033

char s[ran], t[ran];
int slen, tlen;
int dp[ran][ran];
char ans[ran];
int la;

int main() {
	scanf("%s%s", s, t);
	slen = strlen(s);
	tlen = strlen(t);

	for(int i=1;i<=slen;i++)
		for(int j=1;j<=tlen;j++) {
			dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
			if (s[i-1] == t[j-1])
				dp[i][j] = max(dp[i][j], dp[i-1][j-1]+1);
		}
	int x = slen, y = tlen;
	la=0;
	while(x>0 && y>0) {
		if (dp[x][y] == dp[x-1][y]) x--;
		else
		if (dp[x][y] == dp[x][y-1]) y--;
		else {
			ans[la++] = s[x-1];
			x--; y--;
		}
	}
	for(int i=la-1;i>=0;i--)
		putchar(ans[i]);
	puts("");

	return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

const int NMAX = 3005;
int n, m, k;
char s[NMAX], t[NMAX], a[NMAX];
int dp[NMAX][NMAX];

int main()
{
	//ifstream cin("test.in");
	//ofstream cout("test.out");
	cin >> (s + 1);
	cin >> (t + 1);
	n = strlen(s + 1);
	m = strlen(t + 1);
	for (int i = 1;i <= n;++i)
		for (int j = 1;j <= m;++j)
		{
			if (s[i] == t[j])
				dp[i][j] = dp[i - 1][j - 1] + 1;
			else
				dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
		}
	int i = n, j = m;
	k = dp[n][m];
	a[k + 1] = 0;
	while (k > 0)
	{
		if (s[i] == t[j])
		{
			a[k] = s[i];
			--k;
			--i;
			--j;
		}
		else
		{
			if (dp[i - 1][j] > dp[i][j - 1])
				--i;
			else
				--j;
		}
	}
	cout << (a + 1) << "\n";
	return 0;
}


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

int main() {
    int d; cin >> d;

    if (d == 25) { cout << "Christmas" << endl;}
    else if (d == 24) { cout << "Christmas Eve" << endl;}
    else if (d == 23) { cout << "Christmas Eve Eve" << endl;}
    else if (d == 22) { cout << "Christmas Eve Eve Eve" << endl;}

}

Score : 100 points

Problem Statement

In some other world, today is December D-th.

Write a program that prints Christmas if D = 25, Christmas Eve if D = 24, Christmas Eve Eve if D = 23 and Christmas Eve Eve Eve if D = 22.

Constraints

22 \leq D \leq 25

D is an integer.

Input

Input is given from Standard Input in the following format:

D

Output

Print the specified string (case-sensitive).

Sample Input 1

25

Sample Output 1

Christmas

Sample Input 2

22

Sample Output 2

Christmas Eve Eve Eve

Be sure to print spaces between the words.


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

int n;

int main()
{
cin >> n;
int eve = 25-n;
cout << "Christmas";
for (int i = 1; i <= eve; i++)
{
cout << ' ' << "Eve";
}
cout << '\n';
return 0;
}


#include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
#define INF 10e10

using namespace std;

//  void y(void){cout << "yes"<< endl;}
  void Y(void){cout << "YES" << endl;}
  void y(void){cout << "Yes" << endl;}
//  void n(void){cout << "no"<< endl;}
  void N(void){cout << "NO" << endl;}
  void n(void){cout << "No" << endl;}

 void say(string r){cout << r << endl;}
 void sayn(string r){cout << r ;}

	int a,b,c,d,e,f,g,h,ans,res;
//	char a,b,c,d,e,f,g,h,ans,res;
//	string a,b,c,d,e,f,g,h,ans,res;
//	double a,b,c,d,e,f,g,h,ans,res;

void take(int x){
	if(x==1)
		cin >> a;
	else if(x==2)
		cin >> a >> b;
	else if(x==3)
		cin >> a >> b >> c;
	else if(x==4)
		cin >> a >> b >> c >> d;
	else if(x==5)
		cin >> a >> b >> c >> d >> e;
	else if(x==6)
		cin >> a >> b >> c >> d >> e >> f;
	else if(x==7)
		cin >> a >> b >> c >> d >> e >> f >> g;
	else if(x==8)
		cin >> a >> b >> c >> d >> e >> f >> g >> h;
	return ;
}

int main(void){
	take(1);//数

	sayn("Christmas");

	if(a==22){
		say(" Eve Eve Eve");
	//	y();	//Yes
	//	Y();	//YES
	//	n();	//No
	//	N();	//NO
	}

	else if(a==23){

		say(" Eve Eve");
	//	y();
	//	Y();
	//	n();
	//	N();

	}
	else if(a==24){

		say(" Eve");
	//	y();
	//	Y();
	//	n();
	//	N();

	}/*
	else if(){

	//	y();
	//	Y();
	//	n();
	//	N();

	}*/

	else{
		say("");
	//	y();
	//	Y();
	//	n();
	//	N();

	}
//	cout << ans << endl;

	return 0;
}


#include <iostream>
using namespace std;

int main(){
    int D;
    cin >> D;
    if(D == 25) cout << "Christmas" << endl;
    if(D == 24) cout << "Christmas Eve" << endl;
    if(D == 23) cout << "Christmas Eve Eve" << endl;
    if(D == 22) cout << "Christmas Eve Eve Eve" << endl;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	if(n==25){
		cout<<"Christmas"<<endl;
	}
	else if(n==24){
		cout<<"Christmas Eve"<<endl;
	}
	else if(n==23){
		cout<<"Christmas Eve Eve"<<endl;
	}
	else if(n==22){
		cout<<"Christmas Eve Eve Eve"<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define     __        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define     pi        acos(-1)
#define     ll        long long int
#define     f0(n)     for(int i=0;i<n;i++)
#define     mem(a)    memset(a,0,sizeof(a))
#define     mems(a)   memset(a,-1,sizeof(a))

int main()
{
   /* #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif  */
     __

     int d;
     cin>>d;
     if(d==22) cout<<"Christmas Eve Eve Eve"<<endl;
     else if(d==23) cout<<"Christmas Eve Eve\n";
     else if(d==24) cout<<"Christmas Eve\n";
     else cout<<"Christmas\n";

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
#define ll long long
void file(){
#ifndef ONLINE_JUDGE
	freopen("a_input.txt", "r", stdin);
	freopen("a_output.txt", "w", stdout);
#endif
}
void fast()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
int dx[] = { 0, 0, -1, 1, 1, 1, -1, -1 };
int dy[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
int main(){
	//file();
	fast();
	int n;
	cin >> n;
	int sum = 0, mx = 0;
	while (n--){
		int a;
		cin >> a;
		sum += a;
		mx = max(mx, a / 2);
	}
	sum -= mx;
	cout << sum << endl;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<limits>
#include<iomanip>
#include<cmath>
#include<climits>
using namespace std;

#define rp(i,n) for(int i=0;i<n;i++)
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define llrp(i,n) for(long long i=0;i<n;i++)
#define llrep(i,m,n) for(long long i=m;i<=n;i++)
#define sort(a) sort(a.begin(),a.end())
#define reverse(a) reverse(a.begin(),a.end())
#define pb push_back
#define elif else if
#define vcin(a); rp(i,a.size()){cin>>a[i];}
#define vcout(a); rp(i,a.size()){cout<<a[i]<<endl;}
#define allchar(c) for(char c='a';c<='z';c++)

typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef long long ll;
typedef string S;
typedef pair<int,int> P;
typedef queue<int> qi;

const int mod=1e9+7;

ll modpow(ll a,ll b,ll p){
    if(b==0){
        return 1;
    }elif(b%2==0){
        return modpow((a*a)%p,b/2,p);
    }else{
        return (a*modpow(a,b-1,p))%p;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int d;
    cin >> d;
    if(d==25){
        cout << "Christmas" << endl;
    }elif(d==24){
        cout << "Christmas Eve" << endl;
    }elif(d==23){
        cout << "Christmas Eve Eve" << endl;
    }elif(d==22){
        cout << "Christmas Eve Eve Eve" << endl;
    }
    return 0;
}


#include <iostream>
using namespace std;
int main(){
  int d;
  cin>>d;
  switch(d){
    case 25:
      cout<<"Christmas";
      break;
    case 24:
      cout<<"Christmas Eve";
      break;
    case 23:
      cout<<"Christmas Eve Eve";
      break;
      case 22:
          cout<<"Christmas Eve Eve Eve";
          break;
  }
  return 0;}


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

#define fi first
#define se second
#define be begin
#define en end
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ALL(a) (a).be() , (a).en()
#define REP(i,n) for(LL (i)=0;(i)<(n);(i)++)        //repeat n times
#define REP2(i,s,n) for(LL (i)=(s);(i)<(n);(i)++)   //repeat from s to n
#define REPD(i,n) for(int (i)=(n);(i)>=0;(i)--)      //repeat from n to 0
#define REPD2(i,s,e) for(int (i)=(s);(i)>=(e);(i)--) //repeat from s to e
#define RANGE(i,v) for(auto &(i):v)                  //repeat range
#define ASIZE(a) (sizeof(a) / sizeof(a[0]))          //array size

using LL = long long;

template<typename T> using V = vector< T >;
using Vi = V<int>;
using Vll = V<LL>;
using Vs = V<string>;

template<typename T1,typename T2> using M = map< T1, T2>;
using Mii = M<int, int>;
using Msi = M<string, int>;

const int MOD = 1000000007;

int main(){
    LL N;
    cin>>N;
    Vll x(N),y(N),h(N);
    REP(i,N){
        cin>>x[i]>>y[i]>>h[i];
    }

    REP(cx,101){
        REP(cy,101){
            bool ok = true;
            LL H = 0;
            REP(i,N){
                if(h[i] == 0) continue;
                H = h[i]+abs(x[i]-cx)+abs(y[i]-cy);
            }
            REP(i,N){
                LL hi = max(H - abs(x[i]-cx) - abs(y[i]-cy), 0LL);
                if(h[i] != hi) ok = false;
            }
            if(ok) cout<<cx<<" "<<cy<<" "<<H<<endl;
        }
    }

    return 0;
}

Score: 300 points

Problem Statement

In the Ancient Kingdom of Snuke, there was a pyramid to strengthen the authority of Takahashi, the president of AtCoder Inc.

The pyramid had center coordinates (C_X, C_Y) and height H. The altitude of coordinates (X, Y) is max(H - |X - C_X| - |Y - C_Y|, 0).

Aoki, an explorer, conducted a survey to identify the center coordinates and height of this pyramid. As a result, he obtained the following information:

C_X, C_Y was integers between 0 and 100 (inclusive), and H was an integer not less than 1.

Additionally, he obtained N pieces of information. The i-th of them is: "the altitude of point (x_i, y_i) is h_i."

This was enough to identify the center coordinates and the height of the pyramid. Find these values with the clues above.

Constraints

N is an integer between 1 and 100 (inclusive).

x_i and y_i are integers between 0 and 100 (inclusive).

h_i is an integer between 0 and 10^9 (inclusive).

The N coordinates (x_1, y_1), (x_2, y_2), (x_3, y_3), ..., (x_N, y_N) are all different.

The center coordinates and the height of the pyramid can be uniquely identified.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1 h_1
x_2 y_2 h_2
x_3 y_3 h_3
:
x_N y_N h_N

Output

Print values C_X, C_Y and H representing the center coordinates and the height of the pyramid in one line, with spaces in between.

Sample Input 1

4
2 3 5
2 1 5
1 2 5
3 2 5

Sample Output 1

2 2 6

In this case, the center coordinates and the height can be identified as (2, 2) and 6.

Sample Input 2

2
0 0 100
1 1 98

Sample Output 2

0 0 100

In this case, the center coordinates and the height can be identified as (0, 0) and 100.

Note that C_X and C_Y are known to be integers between 0 and 100.

Sample Input 3

3
99 1 191
100 1 192
99 0 192

Sample Output 3

100 0 193

In this case, the center coordinates and the height can be identified as (100, 0) and 193.


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

#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define PI 3.14159265358979323846
#define sz(x) ((int)(x).size())

const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
int main()
{
    int n;
    cin >> n;
    vector<tuple<int, int, int>> vc;
    tuple<int, int, int> G;

    rep(i, n)
    {
        int x, y, h;
        cin >> x >> y >> h;
        vc.push_back(make_tuple(x, y, h));
        if (h > 0)
        {
            G = make_tuple(x, y, h);
        }
    }

    for (int cx = 0; cx < 101; cx++)
    {
        for (int cy = 0; cy < 101; cy++)
        {
            int H = get<2>(G) + abs(cx - get<0>(G)) + abs(cy - get<1>(G));
            bool ok = true;
            rep(i, n)
            {
                int h_temp = max(H - abs(cx - get<0>(vc[i])) - abs(cy - get<1>(vc[i])),0);
                if (h_temp != get<2>(vc[i]))
                {
                    ok = false;
                    break;
                }
            }
            if(ok)
            {
                cout << cx << ' '<< cy << ' '<< H << endl;
                return 0;
            }
        }
    }
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y) debug(x) debug(y);
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))

template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	int n;
	cin >> n;
	vi x(n), y(n), z(n);
	rep(i, n) cin >> x[i] >> y[i] >> z[i];
	rep(i, 101) rep(j, 101){
		//(i, j) is our guess
		int f1 = 1;
		int h;
		int used = -1;
		rep(k, n) if(z[k]){
			h = z[k] + abs(x[k] - i) + abs(y[k] - j);
			used = k;
			break;
		}
		assert(used != -1);
		rep(k, n) if(k != used){
			int al = max(0, h - abs(x[k] - i) - abs(y[k] - j));
			if(al != z[k]){
				f1 = 0;
				break;
			}
		}
		if(f1){
			cout << i << " " << j << " " << h << endl;
			return 0;
		}
	}
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Array bounds
	- Special cases
Be careful!
*/


#include <bits/stdc++.h>
#define pt(sth) cout << sth << "\n"
#define chmax(a, b) {if(a<b) a=b;}
#define chmin(a, b) {if(a>b) a=b;}
#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
static const ll INF=1e18;
static const ll MAX=101010;
static const ll MOD=1e9+7;
static const ll LOGN=55;

int main(void) {
  ll N;
  cin >> N;
  ll x[111], y[111], h[111];
  ll i;

  for(i=0; i<N; i++) {
    cin >> x[i] >> y[i] >> h[i];
  }

  for(ll cx=0; cx<=100; cx++) {
    for(ll cy=0; cy<=100; cy++) {

      ll t=0;
      for(i=0; i<N; i++) {
        if(h[i]==0) continue;
        t=h[i]+abs(x[i]-cx)+abs(y[i]-cy);
      }

      ll f=1;
      for(i=0; i<N; i++) {
        if(h[i]==0) continue;
        ll tt=h[i]+abs(x[i]-cx)+abs(y[i]-cy);

        if(tt!=t) f=0;
      }

      if(f) {
        pt(cx<<" "<<cy<<" "<<t);
        return 0;
      }

    }
  }

}


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

int main(){
    int n;
    cin >> n;

    vector<long long int> x(n), y(n), h(n);
    for(int i=0; i<n; i++){
        cin >> x[i] >> y[i] >> h[i];
    }

    for(int cx=0; cx<=100; cx++){
        for(int cy=0; cy<=100; cy++){
            bool flag = true;
            long long int height = h[0] + abs(x[0] - cx) + abs(y[0] - cy);

            for(int i=1; i<n; i++){
                if(height != h[i] + abs(x[i] - cx) + abs(y[i] - cy)){
                    flag = false;
                    break;
                }
            }

            if(flag == true){
                cout << cx << " " << cy << " " << height << endl;
                return 0;
            }
        }
    }
    return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<math.h>
#include<string>
#include <iomanip>
#include<deque>
#include<queue>
#include<stack>
#include<set>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef int itn;
const ll LINF = 1e18;
const int INF = 1e8;

//マクロ定義
#define vvint(vec,n,m,l) vector<vector<int>> vec(n, vector<int>(m,l));	// lで初期化
#define vvll(vec,n,m,l) vector<vector<ll>> vec(n,vector<ll>(m,l));
#define vint vector<int>
#define pint pair<int,int>
#define rep(i,a) for(int i=0;i<(a);i++)
#define all(x) (x).begin(),(x).end()
#define debug system("pause")				//デバッグ用
#define ret return 0

template<class T>bool chmax(T & a, const T & b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	ll n;
	cin >> n;
	vector<ll> x(n), y(n), h(n);
	rep(i, n)
	{
		cin >> x[i] >> y[i] >> h[i];
	}
	int eq = 0;
	for (int i = 0; i <= 100; i++)
	{
		for (int j = 0; j <= 100; j++)
		{
			ll height = 0;
			for (int k = 0; k < n; k++)
			{
				if (height==0)
				{
					height = max(h[k] + abs(i - x[k]) + abs(j - y[k]),0LL);
					eq++;
				}
				else
				{
					if (height == max(h[k] + abs(i - x[k]) + abs(j - y[k]),0LL))
					{
						eq++;
					}
				}
			}
			if (eq == n)
			{
				cout << i << " " << j << " " << height << endl;
				ret;
			}
			eq = 0;
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
using P = pair<ll, ll>;
#define rep(i, n) for(ll i=0;i<(ll)(n);i++)
#define rep1(i, n) for(ll i=1;i<=(ll)(n);i++)
#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)
#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)
#define ALL(obj) (obj).begin(), (obj).end()
#define MOD 1000000007
#define INF 100000000000

ll d[101][101][101];
ll k[101][101][101];
ll cnt[101][101];

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	ll N;

	cin >> N;

	vector<ll> x(N), y(N), h(N);
	rep(i, N) {
		cin >> x[i] >> y[i] >> h[i];
	}

	ll cx, cy, H;
	ll prev = 0;
	rep(i, 101) {
		rep(j, 101) {
			cnt[i][j] = true;
			rep(n, N) {
				d[n][i][j] = abs(x[n] - i) + abs(y[n] - j);
				if (h[n] != 0) {
					k[n][i][j] = h[n] + d[n][i][j];
					if (n != 0 && prev != k[n][i][j]) {
						cnt[i][j] = false;
						break;
					}
					prev = k[n][i][j];
				}
			}
		}
	}

	rep(i, 101) {
		rep(j, 101) {
			if (cnt[i][j]) {
				rep(n, N) {
					if (h[n] != max(k[n][i][j] - d[n][i][j], 0LL)) {
						cnt[i][j] = false;
					}
				}
			}
		}
	}

	rep(i, 101) {
		rep(j, 101) {
			if (cnt[i][j]) {
				cout << i << " " << j << " " << k[0][i][j] << "\n";
				break;
			}
		}
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
    int N;
    cin >> N;
    std::vector<ll> x(N), y(N), h(N);

    ll xt = 0, yt = 0, ht = 0;
    for (int i = 0; i < N; ++i) {
        cin >> x[i] >> y[i] >> h[i];
        if (h[i] != 0) {
            xt = x[i];
            yt = y[i];
            ht = h[i];
        }
    }

    for (int cx = 0; cx <= 100; ++cx) {
        for (int cy = 0; cy <= 100; ++cy) {
            const auto height = ht + std::abs(cx - xt) + std::abs(cy - yt);
            bool is_ok = true;
            for (int i = 0; i < N; ++i) {
                if (h[i] != std::max(height - std::abs(cx - x[i]) - std::abs(cy - y[i]), 0ll)) {
                    is_ok = false;
                }
            }
            if (is_ok) {
                cout << cx << " " << cy << " " << height << endl;
                return 0;
            }
        }
    }

    return 0;
}


#include <iostream>
#include <cmath>

long long x[100][3];
int N;

using namespace std;

int main(){
    cin >> N;
    for(int i=0;i<N;i++) cin >> x[i][0] >> x[i][1] >> x[i][2];
    for(long long i=0;i<=100;i++){
        for(long long j=0;j<=100;j++){
            bool flag = true;
            long long H;
            int t=0;
            for(;t<N;t++){
                if(x[t][2] != 0){
                    H = abs(i-x[t][0]) + abs(j-x[t][1]) + x[t][2];
                    break;
                }
            }
            for(int k=t+1;k<N;k++){
                if(H != abs(i-x[k][0]) + abs(j-x[k][1]) + x[k][2]){
                    flag = false;
                    break;
                }
            }
            if(flag){
                cout << i << " " << j << " " << H << endl;
                return 0;
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define INF 1e9;
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int, int> P;

int main() {
    int n; cin >> n;
    vector<int> x(n), y(n), h(n);
    rep(i, n) cin >> x[i] >> y[i] >> h[i];

    rep(cx, 101) {
        rep(cy, 101) {
            //int H = max(cx-x[0], 0) + max(cy-y[0], 0) + h[0];
            int cnt = 0;
            int H;
            bool a = true;
            rep(i, n) {
                if (h[i] > 0) {
                    if (cnt == 0) {
                        H = abs(cx-x[i]) + abs(cy-y[i]) + h[i];
                        cnt++;
                        continue;
                    }
                    int tmp = abs(cx-x[i]) + abs(cy-y[i]) + h[i];
                    if (H != tmp) {
                        a = false;
                        break;
                    }
                } else {
                    ///////
                    if (cnt != 0) {
                        if (H > abs(cx-x[i])+abs(cy-y[i])+h[i]) break;
                    }
                }

            }
            if (a) {
                cout << cx << " " << cy << " " << H << endl;
            }

        }
    }

}


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

#define MAX 101
long long tile[MAX][MAX];
tuple<long long,long long,long long> data[MAX];
long long n;
// プロトタイプ宣言
void Solve(void);
// ここまで

int main(){
	cin>>n;
	long long x,y,h;
	for(long long i=0;i<n;i++){
		cin>>x>>y>>h;
		data[i]=make_tuple(x,y,h);
	}

	long long ansx=0, ansy=0, ansh=0;
	if(n==1){
		ansx=get<0>(data[0]);
		ansy=get<1>(data[0]);
		ansh=get<2>(data[0]);

	}else{
		Solve();
		bool flag=false;

		for(long long y=0;y<MAX;y++){
			for(long long x=0;x<MAX;x++){
				if(tile[y][x]){
					flag=true;
					ansx=x;
					ansy=y;
					ansh=tile[y][x];
					break;
				}
			}
			if(flag)break;
		}
	}
	printf("%d %d %d\n",ansx,ansy,ansh);

	return 0;
}

void Solve(void){
	// 初期化 {{{
	for(long long i=0;i<MAX;i++){
		for(long long j=0;j<MAX;j++){
			tile[i][j]=0;
		}
	}
	// }}}
	// 解答 {{{
	// 答えになるかもしれない座標はせいぜい101x101個なので全部試してみる
	bool flag=true;
	for(long long i=0;i<MAX;i++){	// マス目:y
		for(long long j=0;j<MAX;j++){ // マス目:x
			bool flag=true;

			// 初期設定
			long long sx=get<0>(data[0]);
			long long sy=get<1>(data[0]);
			long long sh=get<2>(data[0]);
			tile[i][j]= sh + abs(j-sx) + abs(i-sy);
			/*
			   とりあえず、現座標の高さを、
			   『1個目のデータから求められる高さ』と置いてみる
			 */

			for(long long k=1;k<n;k++){
				long long x=get<0>(data[k]);
				long long y=get<1>(data[k]);
				long long h=get<2>(data[k]);

				long long height = h + abs(j-x) + abs(i-y);
				// 2個目以降のデータによって求められる、現座標の高さ
				if(height!=tile[i][j]){
					flag=false;
					// 最初に置いた高さと height が違うなら、そこは正解じゃないよね！！
					break;
				}
			}
			if(flag==false){
				tile[i][j]=0;
				continue;
			}
		}
	}
	// }}}
}


#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define rep(i, s, N) for(ll i{s}; i < N; i++)
#define rem(i, N, s) for(ll i{N}; i > s; i--)
using namespace std;using ll = long long int;using ld = long double;using P = pair<ll,ll>;const int MOD = (int)1e9+7;const string rt = "\n";const string sp = " ";/* 最大公約数 */template<typename T> T gcd(T a, T b){return b != 0 ? gcd(b, a % b) : a;}/* UnionFind */template<typename T> struct UnionFind{vector<T> par;UnionFind(T n) : par(n, -1){}void init(T n){par.assign(n, -1);}T root(T x){if (par[x] < 0) return x;else return par[x] = root(par[x]);}bool issame(T x, T y){return root(x) == root(y);}bool merge(T x, T y){x = root(x); y = root(y);if(x == y) return false;if(par[x] > par[y]) swap(x, y);par[x] += par[y];par[y] = x;return true;}int size(int x) {return -par[root(x)];}};/* コンビネーション */template<typename T> ll combpm(T N_, T C_) {const int NUM_=400001;static ll fact[NUM_+1],factr[NUM_+1],inv[NUM_+1];if (fact[0]==0) {inv[1]=fact[0]=factr[0]=1;for (int i=2;i<=NUM_;++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;for (int i=1;i<=NUM_;++i) fact[i]=fact[i-1]*i%MOD, factr[i]=factr[i-1]*inv[i]%MOD;}if(C_<0 || C_>N_) return 0;return factr[C_]*fact[N_]%MOD*factr[N_-C_]%MOD;}/* 多次元ベクター */template<class T>vector<T> mvec(size_t a){return vector<T>(a);}template<class T, class... Ts>auto mvec(size_t a, Ts... ts){return vector<decltype(mvec<T>(ts...))>(a, mvec<T>(ts...));}/* テスト関数 */void test(ll n){cout << "test" << n << endl;}/* 小数点以下 */void fixsp(ll n){cout << fixed << setprecision(n);}void defsp(ll n){cout << defaultfloat << setprecision(n);}/* 重み付きUnionFind */struct WUnionFind {vector<int> par;vector<int> rank;WUnionFind(int n = 1) {init(n);}void init(int n = 1) {par.resize(n); rank.resize(n);for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0;}int root(int x) {if (par[x] == x) {return x;}else {int r = root(par[x]);return par[x] = r;}}bool issame(int x, int y) {return root(x) == root(y);}bool merge(int x, int y) {x = root(x); y = root(y);if (x == y) return false;if (rank[x] < rank[y]) swap(x, y);if (rank[x] == rank[y]) ++rank[x];par[y] = x;return true;}};/* IO高速化 */void fastio(){ios::sync_with_stdio(false);cin.tie(NULL);}
/* ここからコード開始 */

int main(){
  fastio;
  ll N;
  cin >> N;
  auto xyh = mvec<ll>(0,3);
  ll hmin{MOD};
  rep(i,0,N){
    ll x, y, h;
    cin >> x >> y >> h;
    hmin = min(hmin, h);
    xyh.push_back({x, y, h});
  }
  rep(x, 0, 101) rep(y, 0, 101) rep(h, hmin, hmin+201){
    bool isanswer{true};
    for(auto&& i : xyh){
      if(abs(x-i[0])+abs(y-i[1])+i[2] != h) isanswer = false;
    }
    if(isanswer){
      cout << x << sp << y << sp << h << rt;
      return 0;
    }
  }
}


#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define whlie while
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define inf 1001001001
#define mod 1000000007
#define FOR(i,a,b) for(int (i)=((int)a); (i)<((int)b); (i)++) // [a,b)
#define rep(i,N) FOR((i), 0, ((int)N)) // [0,N)
#define FORR(i,a,b) for(int (i)=((int)b) - 1; (i)>=((int)a); (i)--)
#define repr(i,N) FORR((i), 0, ((int)N))
#define all(v) (v).begin(),(v).end()
#define sz(v) ((int)(v).size())
#define vrep(v,it) for(auto it=v.begin();it<v.end();it++)
#define vrepr(v,it) for(auto it=v.rbegin();it<v.rend();it++)
#define inx(t,...) t __VA_ARGS__; in(__VA_ARGS__)
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) ll __VA_ARGS__; in(__VA_ARGS__)
#define inc(...) char __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define ind(...) double __VA_ARGS__; in(__VA_ARGS__)
#define inpii(...) pii __VA_ARGS__; in(__VA_ARGS__)
#define invi(v,...) vi v; in(v,##__VA_ARGS__)
#define invl(v,...) vl v; in(v,##__VA_ARGS__)

// デバッグ用
// dbg(...) printfと同様に使用可能
// trc(var) (変数名)=(値)と表示
// stopif(val) 条件分が真の時に止まる
// vd(v,N) vの中身を表示

#ifdef LOCAL
    #define dbg(...) fprintf(stderr, __VA_ARGS__)
    #define trc(...) do{cout << #__VA_ARGS__ << " = "; dbg_out(__VA_ARGS__);}while(0)
    #define stopif(val) assert( !(val) )
    #define vdbg(v,...) do{cout << #v << " = "; vector_debug(v , ##__VA_ARGS__);}while(0)
#else
    #define dbg(...) 1
    #define trc(...) 1
    #define stopif(...) 1
    #define vdbg(...) 1
#endif

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector< vector<int> > vvi;
int gcd(int a, int b){if(a>b) swap(a,b); return a==0 ? b : gcd(b%a,a);} ll gcd(ll a, ll b){if(a>b) swap(a,b); return a==0 ? b : gcd(b%a,a);}
int lcm(int a, int b){return (a / gcd(a,b)) * b;} ll lcm(ll a, ll b){return (a / gcd(a,b)) * b;}
inline int pow(int a, int b){int ans = 1; rep(i,b) ans *= a; return ans;} inline ll pow(ll a, ll b){ll ans = 1; rep(i,b) ans*= a; return ans;}
inline ll pow(int a, ll b){ll ans = 1; rep(i,b) ans*= a; return ans;} inline ll pow(ll a, int b){ll ans = 1; rep(i,b) ans*= a; return ans;}
template<typename T, typename U> inline bool amin(T &x, U y) { return (y < x) ? (x = y, true) : false; }
template<typename T, typename U> inline bool amax(T &x, U y) { return (x < y) ? (x = y, true) : false; }
template<typename C> inline void _cin(C &c){cin >> c;}
template<typename T,typename U> inline void _cin(pair<T,U> &p){cin >> p.fi; cin >> p.se;}
template<typename C> inline void _cout(const C &c){cout << c;}
template<typename T,typename U> inline void _cout(const pair<T,U> &p){cout << p.fi << ' ' << p.se;}
void in(){} template <typename T,class... U> void in(T &t,U &...u){ _cin(t); in(u...);}
void out(){cout << endl;} template <typename T,class... U> void out(const T &t,U ...u){ _cout(t); if(sizeof...(u)) cout << ' '; out(u...);}
template<typename C> inline void in(vector<C> &v,int N=-1){if(sz(v) != 0){int M=(N == -1) ? sz(v) : N; rep(i,M) _cin(v[i]);}else{C c;rep(i,N) v.pb((_cin(c),c));}}
template<typename C> inline void in(C v[],int N){rep(i,N) _cin(v[i]);}
template<typename C> inline void out(const vector<C> &v,int N=-1){int M=(N == -1) ? sz(v) : N; rep(i,M) {cout<<( (i)?" ":"" ); _cout(v[i]);} cout<<endl; }
template<typename C> inline void out(C v[],int N){rep(i,N) {cout<<( (i)?" ":"" ); _cout(v[i]);} cout<<endl; }
template<typename C> inline void vector_debug(const vector<C> &v,int N=-1){cout << "{"; int M=(N == -1) ? sz(v) : N; rep(i,M) {cout<<( (i)?", ":"" ); _cout(v[i]);} cout<<"}"; }
template<typename C> inline void vector_debug(C v[], int N){rep(i,N) {cout << "{"; cout<<((i)?", ":""); _cout(*(v+i));} cout<<"}";}
void dbg_out(){cout << endl;} template <typename T,class... U> void dbg_out(const T &t,U ...u){ _cout(t); if(sizeof...(u)) cout << ", "; dbg_out(u...);}
template<typename C,class... U> void dbg_out(const vector<C> &v,U ...u){vector_debug(v); if(sizeof...(u)) cout << ", "; dbg_out(u...);}
template<typename C,class... U> void dbg_out(const vector<vector<C>> &v,U ...u){cout << "{ "; rep(i,sz(v)) {if(i)cout<<", "; vector_debug(v[i]);} cout << " }"; if(sizeof...(u)) cout << ", "; dbg_out(u...);}
template<typename C> inline C vmax(const vector<C> &v){C n=v[0]; rep(i,sz(v)) amax(n,v[i]); return n;} template<typename C> inline C vmax(C v[], int N){C n=v[0]; rep( i , N ) amax(n,v[i]); return n;}
template<typename C> inline C vmin(const vector<C> &v){C n=v[0]; rep(i,sz(v)) amin(n,v[i]); return n;} template<typename C> inline C vmin(C v[], int N){C n=v[0]; rep( i , N ) amin(n,v[i]); return n;}
template<typename C> inline C vsum(const vector<C> &v){C n=0; rep(i,sz(v)) n+=v[i]; return n;} template<typename C> inline C vsum(C v[], int N){C n=0; rep( i , N ) n+=v[i]; return n;}

// ini(A,B,C) => int A,B,C; cin >> A >> B >> C;
// in(A,B,C) => cin >> A >> B >> C;   invi(v,N) =>vi v; rep(i,N) v.pb( (int x,cin >> x,x) );
// out(A,B,C) cout << A << ' ' <<  B << ' ' << C;
// in,outともにベクトル・配列・pairに使用可能(ただしベクトル・配列は１つしか入れられない)
// amax(x,y) bool関数　x<yの時にxにyを代入してtrueを返す

int main(){
    ini(N);
    vi x(N),y(N),h(N);
    rep(i,N) in(x[i],y[i],h[i]);
    rep(i,101) rep(j,101){
        int h_ans = -1;
        int flg = 1;
        rep(k,N){
            if(h[k] == 0)continue;
            int h_cur = h[k]+abs(x[k]-i)+abs(y[k]-j);
            if(h_ans == -1) h_ans = h_cur;
            if(h_ans != h_cur){
                flg = 0; break;
            }
        }
        if(flg){
            out(i,j,h_ans);
            return 0;
        }
    }
}


#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;
typedef long long ll;
const int MAXN = 110;

struct pos {
  ll x, y, h;
};

bool compare(const pos& lhs, const pos& rhs) {
  return lhs.h > rhs.h;
}

int n;
pos positions[MAXN];

bool check(int cx, int cy, int& h) {
  h = positions[0].h + abs(positions[0].x - cx) + abs(positions[0].y - cy);
  for (int i=1;i<n;i++) {
    if (positions[i].h != max<ll>(h - abs(positions[i].x - cx) - abs(positions[i].y - cy), 0)) {
      return false;
    }
  }
  return true;
}

void solve() {
  sort(positions, positions + n, compare);
  for (int x=0;x<=100;x++) {
    for (int y=0;y<=100;y++) {
      int h;
      if (check(x, y, h)) {
        cout << x << " " << y << " " << h << endl;
        return;
      }
    }
  }
}

int main() {
  cin >> n;
  for (int i=0;i<n;i++) {
    cin >> positions[i].x >> positions[i].y >> positions[i].h;
  }
  solve();
}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <utility>
using namespace std;

typedef long long ll;
ll MOD = 1e9+7;

int n;
int x[100];
int y[100];
ll h[100];

bool fff(int cx,int cy,ll ch)
{
	for(int i=0;i<n;i++)
	{
		ll ih=max(ch-abs(cx-x[i])-abs(cy-y[i]),0LL);
		if(ih!=h[i])
			return false;
	}
	return true;
}

int main()
{
	int ans=0;
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>x[i]>>y[i]>>h[i];

	//最大高さを調べる
	ll mh=0;
	for(int i=0;i<n;i++)
		mh=max(mh,h[i]);

	//それぞれの座標ごとにmh+200まで調べる
	for(int i=0;i<101;i++)
	{
		for(int j=0;j<101;j++)
		{
			for(ll k=0;k<201;k++)
			{
				if(fff(i,j,k+mh))
				{
					cout<<i<<" "<<j<<" "<<k+mh<<endl;
					return 0;
				}
			}
		}
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <random>
#include <chrono>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define INF 1000000000
#define MOD 1000000007

int main(){
    int N; cin >> N;
    vector<vector<int>> T(N, vector<int>(3));

    for(vector<int> &t:T)
        cin >> t[0] >> t[1] >> t[2];

    map<int, int> M;
    vector<vector<int>> o;
    for(int i=0; i<=100; i++){
        for(int j=0; j<=100; j++){
            for(int k=0; k<N; k++){
                if(T[k][2]==0) continue;
                M[T[k][2]+abs(i-T[k][0])+abs(j-T[k][1])]++;
            }
            if(M.size()==1) o.push_back({i, j, M.begin()->first});
            M.clear();
        }
    }

    for(int i=0; i<o.size(); i++){
        bool f = true;
        for(int j=0; j<N; j++){
            if(T[j][2]==0 && max(0, o[i][2] - abs(o[i][0]-T[j][0]) - abs(o[i][1]-T[j][1]))!=0) f = false;
        }
        if(f) cout << o[i][0] << " " << o[i][1] << " " << o[i][2] << endl;
    }
    return 0;
}


#include<iostream>
#include<math.h>
#include<stdlib.h>
using namespace std;
int main(){
    int n,i,j,k,kk,f,ff;
    int a[110],b[110],c[110];
    cin >> n;
    for(i=0;i<n;i++)
        cin >> a[i] >> b[i] >> c[i];
    for(i=0;i<=100;i++)
    {
        for(j=0;j<=100;j++)
        {
            f=0;ff=0;
            kk=c[0]+abs(a[0]-i)+abs(b[0]-j);
            if(c[0]==0)
                ff=1;
            for(k=1;k<n;k++)
            {
                if(c[k]==0)
                    continue;
                if(ff)
                {
                    kk=c[k]+abs(a[k]-i)+abs(b[k]-j);
                    ff=0;
                }
                else if(kk!=c[k]+abs(a[k]-i)+abs(b[k]-j))
                {
                    f=1;
                    break;
                }
            }
            if(f==0)
            {
                cout << i << " " << j << " " << kk << endl;
                return 0;
            }
        }
    }
}


#include <iostream>
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <utility>
#include <fstream>
#include <random>
#include <map>
#include <unordered_map>
#include <cstdlib>
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
#define p pair<int,int>
#define reverse(s) reverse(s.begin(), s.end());
int dx[4] = { 0,1,0,-1 };
int dy[4] = { 1,0,-1,0 };
using namespace std;
int POW(int x, int y) { return int(pow(double(x), double(y))); }

int main() {
	int n;
	cin >> n;
	vector<int>x(n), y(n), h(n);
	rep(i, n) {
		cin >> x[i] >> y[i] >> h[i];
	}
	rep(i,101){
		rep(j, 101) {
			bool flg = 1;
			int ansh = 0;
			vector<int> ika;
			rep(k, n) {
				if (h[k] > 0) {
					int h0 = h[k] + abs(i - x[k]) + abs(j - y[k]);
					if (h0 > 0) {
						if (ansh == 0) {
							ansh = h0;
						}
						else {
							if (ansh != h0) {
								flg = 0;
							}
						}
					}
				}
				else {
					int h0 = h[k] + abs(i - x[k]) + abs(j - y[k]);
					ika.push_back(h0);
				}
			}
			if (ansh>0) {
				rep(i, ika.size()) {
					if (ansh < ika[i]) {
						flg = 0;
					}
				}
				if (flg == 1) {
					cout << i << " " << j << " " << ansh << endl;
					return 0;
				}
			}
		}
	}
	vector<int> cc(1);
	cc[1] = 0;
	return 0;
}