Language: C++

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <map>
#include <set>
#include <stack>
#include <queue>
 
using namespace std;
 
#define pb(n)	push_back(n)
#define mp(n,m) make_pair(n,m)
#define fi 	first
#define se 	second
#define all(r) (r).begin(),(r).end()
 
#define REP(i,s,e)	for(int i=(s); i<(e); i++)
#define rep(i,n)	REP(i,0,n)
#define REPE(i,s,e)	for(int i=s; i>e; i--)
#define repe(i,n)	REPE(i,n,-1)


typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
typedef vector<vi> vvi;
 
const int IMAX=((1<<30)-1)*2+1;
const int INF=100000000;
const double EPS=1e-10;
//int mod=1000000007



const int MAX_N = 1000;
const int MAX_W = 200000000;

int dp[MAX_N + 1][MAX_W + 1][5];
int v[MAX_N], w[MAX_N];
int N,W;

// int solve(int i, int j, int k){
// 	if(dp[i][j][k] != -1) return dp[i][j][k];
// 	int ret = 0;
// 	if(i == N) ret = 0;
// 	else if(j < w[i] || k == 4) ret =dp[i][j][k];
// 	else ret =max(max(solve(i,j,k),solve(i,j-w[i],k+1) + v[i]),solve(i+1,j-w[i],k + 1) + v[i]);
// 	return dp[i][j][k] = ret;
// }

int solve() {
	for(int i = 0; i < N; i++) {
		for(int j = 0; j <= W; j++){
			for(int k = 0; k < 5; k++){
				if(j < w[i] || k <1) {
					dp[i + 1][j][k] = dp[i][j][k];
				}
				else{
					dp[i + 1][j][k] = max(max(dp[i][j][k],dp[i][j-w[i]][k-1] + v[i]), dp[i + 1][j - w[i]][k-1] + v[i]);
				}
			}
		}
	}
	return dp[N][W][4];
}

int main(){
	int n, m;
	while(cin>>n>>m){
		N = n;
		W = m;
		// for(int i = 0; i<= N; i++){
		// 	for(int j = 0; j <=W; j++){
		// 		for(int k = 0; k < 4; k++){
		// 			dp[i][j][k] = -1;
		// 		}
		// 	}
		// }
		for(int i = 0; i < n; i++){
			cin >> v[i];
			w[i] = v[i];
		}
		cout<<solve()<<endl;
	}
}

Compiler output:
/tmp/ccVt5TpW.o: in function `solve()':
a.cc:(.text+0x3c): relocation truncated to fit: R_X86_64_PC32 against symbol `w' defined in .bss section in /tmp/ccVt5TpW.o
a.cc:(.text+0xe1): relocation truncated to fit: R_X86_64_PC32 against symbol `w' defined in .bss section in /tmp/ccVt5TpW.o
a.cc:(.text+0x135): relocation truncated to fit: R_X86_64_PC32 against symbol `v' defined in .bss section in /tmp/ccVt5TpW.o
a.cc:(.text+0x151): relocation truncated to fit: R_X86_64_PC32 against symbol `w' defined in .bss section in /tmp/ccVt5TpW.o
a.cc:(.text+0x1a7): relocation truncated to fit: R_X86_64_PC32 against symbol `v' defined in .bss section in /tmp/ccVt5TpW.o
a.cc:(.text+0x262): relocation truncated to fit: R_X86_64_PC32 against symbol `W' defined in .bss section in /tmp/ccVt5TpW.o
a.cc:(.text+0x275): relocation truncated to fit: R_X86_64_PC32 against symbol `N' defined in .bss section in /tmp/ccVt5TpW.o
a.cc:(.text+0x284): relocation truncated to fit: R_X86_64_PC32 against symbol `N' defined in .bss section in /tmp/ccVt5TpW.o
a.cc:(.text+0x28a): relocation truncated to fit: R_X86_64_PC32 against symbol `W' defined in .bss section in /tmp/ccVt5TpW.o
/tmp/ccVt5TpW.o: in function `main':
a.cc:(.text+0x2ce): relocation truncated to fit: R_X86_64_PC32 against symbol `N' defined in .bss section in /tmp/ccVt5TpW.o
a.cc:(.text+0x2d7): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status