#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<climits>
using namespace std;
struct tree{
public:
int val;
tree* l;
tree* r;
tree(){};
tree(int val):val(val),l(nullptr),r(nullptr){}
};
tree* build(vector<int>& nums){
int v=*max_element(nums.begin(),nums.end());
int i=0;
for(auto vt:nums){
if(vt==v){
break;
}
i++;
}
tree* root=new tree(v);
vector<int> L(nums.begin(),nums.begin()+i),R(nums.begin()+i+1,nums.end());
if(!L.empty()) root->l=build(L);
if(!R.empty()) root->r=build(R);
return root;
}
void pre(tree* root,vector<int>& num){
if(!root){
num.push_back(-1);
return ;
}
num.push_back(root->val);
if(root->l || root->r){
pre(root->l,num);
pre(root->r,num);
}
}
int main(){
int n;
vector<int> nums;
while(cin>>n){
nums.push_back(n);
}
tree* root=build(nums);
vector<int> num;
pre(root,num);
for(int i=0;i<num.size();i++){
if(!i){
cout<<num[i];
}
else if(num[i]==-1){
cout<<" null";
}
else{
cout<<" "<<num[i];
}
}
return 0;
}
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<climits>
using namespace std;
int f(vector<int>& nums){
int a=nums[0],count=0;
for(int i=0;i<nums.size();i++){
if(count==0) a=nums[i];
if(nums[i]==a) count++;
else count--;
}
return a;
}
int main(){
int n;
cin>>n;
vector<int> nums(n);
for(int i=0;i<n;i++){
cin>>nums[i];
}
cout<<f(nums)<<endl;
}
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<climits>
using namespace std;
int f(vector<int>& nums,int n){
int max=0;
for(int i=0;i<n;i++){
int sum=0;
for(int j=i;j<n;j++){
sum+=nums[j];
if(sum>max){
max=sum;
}
}
}
return max;
}
int main(){
int n,i;
cin>>n;
vector<int> nums(n);
for(int i=0;i<n;i++){
cin>>nums[i];
}
cout<<f(nums,n)<<endl;
}
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<climits>
using namespace std;
vector<int> f(vector<int>& nums,int k){
vector<int> res;
sort(nums.begin(),nums.end());
for(int i=0;i<k;i++){
res.push_back(nums[i]);
}
return res;
}
int main(){
int n,k;
cin>>n>>k;
vector<int> nums(n),res;
for(int i=0;i<n;i++){
cin>>nums[i];
}
res=f(nums,k);
for(int i=0;i<res.size();i++){
cout<<res[i]<<" ";
}
}
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<climits>
using namespace std;
int f(vector<int>& nums,int k){
sort(nums.begin(),nums.end(),greater<int>());
return nums[k-1];
}
int main(){
int n,k;
cin>>n>>k;
vector<int> nums(n);
for(int i=0;i<n;i++){
cin>>nums[i];
}
cout<<f(nums,k)<<endl;
}
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<climits>
using namespace std;
int f(int m,int n){
if(m==1 || n==1) return 1;
if(m==2 && n==2) return 2;
else return f(m-1,n)+f(m,n-1);
}
int main(){
int m,n;
cin>>m>>n;
cout<<f(m,n)<<endl;
}
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<climits>
using namespace std;
int f(int n){
if(n==1 || n==2 || n==3) return n;
if(n>3){
int a=1;
int b=2;
int temp=0;
for(int i=3;i<n+1;i++){
temp=a+b;
a=b;
b=temp;
}
return temp;
}
}
int main(){
int n;
cin>>n;
cout<<f(n)<<endl;
}
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<climits>
using namespace std;
long long f(int N,int C, vector<int>& w,vector<int>& v){
vector<vector<long long >> dp(N+1,vector<long long>(C+1,0));
for(int i=1;i<=N;i++){
for(int j=1;j<=C;j++){
if(w[i]<=j){
dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]);
}
else{
dp[i][j]=dp[i-1][j];
}
}
}
return dp[N][C];
}
int main(){
int N;int C;
cin>>N>>C;
vector<int> w(N+1),v(N+1);
for(int i=1;i<=N;++i){
cin>>w[i];
}
for(int i=1;i<=N;++i){
cin>>v[i];
}
cout<<f(N,C,w,v)<<endl;
}
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<climits>
using namespace std;
string f(string s){
int max=0;
int size=s.size();
vector<vector<int>> dp(size,vector<int>(size));
string ret=" ";
for(int i=0;i<size;i++){
for(int j=0;j<=i;j++){
dp[i][j]=s[i]==s[j] && (i-j<=2 || dp[i-1][j+1]);
if(dp[i][j]){
if(i-j+1>max){
max=i-j+1;
ret=s.substr(j,i-j+1);
}
}
}
}
return ret;
}
int main(){
string s;
cin>>s;
cout<<f(s)<<endl;
}
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<climits>
using namespace std;
int f(vector<int>& nums){
if(nums.empty()) return 0;
int a=INT_MIN;
int b=0;
for(int i=0;i<nums.size();i++){
b+=nums[i];
if(b>a) a=b;
if(b<0) b=0;
}
return a;
}
int main(){
int n,i;
cin>>n;
vector<int> nums(n);
while(cin>>i){
nums.push_back(i);
if(cin.get()=='\n') break;
}
cout<<f(nums)<<endl;
}
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<climits>
using namespace std;
int f(string f1,string f2){
int a=f1.size();
int b=f2.size();
vector<vector<int>> dp(a+1,vector<int>(b+1));
for(int i=1;i<=a;i++){
char c1=f1.at(i-1);
for(int j=1;j<=b;j++){
char c2=f2.at(j-1);
if(c1==c2){
dp[i][j]=dp[i-1][j-1]+1;
}
else{
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
}
return dp[a][b];
}
int main(){
string a,b;
cin>>a>>b;
cout<<f(a,b)<<endl;
}
class Solution:
def f(self,bed,n):
for i in range (1,len(bed)-1):
if sum(bed[i-1 : i+2]) ==0:
bed[i]=1;
n-=1
return n<=0
if __name__=="__main__":
solution=Solution()
bed=list(map(int,input().split()))
n=int(input())
result=solution.f(bed,n)
if result:
print("true")
else:
print("false")
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<climits>
using namespace std;
int f(int n){
vector<int> dp(n+1);
dp[0]=0;
dp[1]=0;
for(int i=3;i<=n;i++){
for(int j=1;j<i;j++){
dp[i]=max(dp[i],max(j*(i-j),j*dp[i-j]));
}
}
return dp[n];
}
int main(){
int n;
cin>>n;
cout<<f(n)<<endl;
}
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<climits>
using namespace std;
int f(vector<int>& nums){
int size=nums.size();
vector<int> dp(size);
dp[0]=nums[0];
dp[1]=max(nums[0],nums[1]);
if(size==0) return 0;
if(size==1) return nums[0];
for(int i=2;i<size;i++){
dp[i]=max(dp[i-1],dp[i-2]+nums[i]);
}
return dp[size-1];
}
int main(){
int i;
vector<int> nums;
while(cin>>i){
nums.push_back(i);
if(cin.get()=='\n') break;
}
cout<<f(nums)<<endl;
}
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<climits>
using namespace std;
int f(vector<int>& nums){
nums.push_back(1);
nums.emplace(nums.begin(),1);
int size=nums.size();
int dp[size][size];
for(int i=size-1;i>=0;i--){
for(int j=i;j<size;j++){
int max=0;
for(int k=i+1;k<j;k++){
int temp=dp[i][k]+dp[k][j]+nums[i]*nums[k]*nums[j];
if(temp>max){
max=temp;
}
}
dp[i][j]=max;
}
}
return dp[0][size-1];
}
int main(){
int i;
vector<int> nums;
while(cin>>i){
nums.push_back(i);
if(cin.get()=='\n') break;
}
cout<<f(nums)<<endl;
}