Language: C++

#include <stdio.h>  
#include <algorithm>  
#include <assert.h>
#include <bitset>
#include <cmath>  
#include <complex>  
#include <deque>  
#include <functional>  
#include <iostream>  
#include <limits.h>  
#include <map>  
#include <math.h>  
#include <queue>  
#include <set>  
#include <stdlib.h>  
#include <string.h>  
#include <string>  
#include <time.h>  
#include <unordered_map>  
#include <unordered_set>  
#include <vector>  
#include <chrono>
#include <random>
#include <time.h>
#include <fstream>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue<int>
#define pqg priority_queue<int,vector<int>,greater<int>>
#define pb push_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define endl "\n"
#define all(c) begin(c),end(c)
using namespace std;
int in() {int x;scanf("%d",&x);return x;}
ll lin() {ll x;scanf("%lld",&x);return x;}

#define INF 1e9+7
#define LLINF 1e18+7
#define N 250000
ll MOD=1e9+7;

int makeable[2010][1001000];
vector<pair<string,int>> dp(1000010,pair<string,int>{"{",-1});
vector<string> s;
vector<int> a;

main(){
  int n=in(),k=in();
  rep(i,n){
    string S;cin>>S;
    s.pb(S);
    a.pb(S.size());
  }
  dp[k].first="";
  makeable[n][k]=1;
  rep3(i,n-1,0){
    int x=a[i];
    rep(j,k+1){
      makeable[i][j]=(makeable[i+1][j]|makeable[i+1][j+x]);
      if(makeable[i+1][j+x]){
        string temp=s[i]+dp[j+x].first;
        dp[j].first=min(temp,dp[j].first);
      }
    }
  }
  cout<<dp[0].first<<endl;
  return 0;
  /*
  rep(i,n+1){
    rep(j,k+1){
      cout<<makeable[i][j]<<" ";
    }
    cout<<endl;
  }
  */
  string best;
  rep(i,k+1){
    string temp=dp[i].first;
    int index=dp[i].second;
    rep2(j,index+1,n-1){
      if(makeable[j][i]==0)continue;
      if(i+a[j]>k)continue;
      string t=temp+s[j];
      if(dp[i+a[j]].first>t){
        dp[i+a[j]].first=t;
        dp[i+a[j]].second=j;
      }
    }
    /*
    rep(i,k+1){
      cout<<dp[i].first<<" ";
    }
    cout<<endl;
    */
  }
  cout<<dp[k].first;
}


Compiler output:
a.cc:54:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   54 | main(){
      | ^~~~
/tmp/ccN2F5Di.o: in function `main':
a.cc:(.text+0xa0): relocation truncated to fit: R_X86_64_PC32 against symbol `s[abi:cxx11]' defined in .bss section in /tmp/ccN2F5Di.o
a.cc:(.text+0xc5): relocation truncated to fit: R_X86_64_PC32 against symbol `a' defined in .bss section in /tmp/ccN2F5Di.o
a.cc:(.text+0xf8): relocation truncated to fit: R_X86_64_PC32 against symbol `dp[abi:cxx11]' defined in .bss section in /tmp/ccN2F5Di.o
a.cc:(.text+0x15f): relocation truncated to fit: R_X86_64_PC32 against symbol `a' defined in .bss section in /tmp/ccN2F5Di.o
a.cc:(.text+0x24a): relocation truncated to fit: R_X86_64_PC32 against symbol `dp[abi:cxx11]' defined in .bss section in /tmp/ccN2F5Di.o
a.cc:(.text+0x263): relocation truncated to fit: R_X86_64_PC32 against symbol `s[abi:cxx11]' defined in .bss section in /tmp/ccN2F5Di.o
a.cc:(.text+0x28e): relocation truncated to fit: R_X86_64_PC32 against symbol `dp[abi:cxx11]' defined in .bss section in /tmp/ccN2F5Di.o
a.cc:(.text+0x2b9): relocation truncated to fit: R_X86_64_PC32 against symbol `dp[abi:cxx11]' defined in .bss section in /tmp/ccN2F5Di.o
a.cc:(.text+0x30b): relocation truncated to fit: R_X86_64_PC32 against symbol `dp[abi:cxx11]' defined in .bss section in /tmp/ccN2F5Di.o
/tmp/ccN2F5Di.o: in function `__static_initialization_and_destruction_0()':
a.cc:(.text+0x3c9): relocation truncated to fit: R_X86_64_PC32 against symbol `dp[abi:cxx11]' defined in .bss section in /tmp/ccN2F5Di.o
a.cc:(.text+0x3fb): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status