/*
跟1030一样,只不过多加了一项限制,当minneed==tempneed的时候再判断一下minback即可;
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int cmax, n, sp, m;
int e[510][510], dis[510], weight[510];
vector<int> pre[510]; //记录s到d的路径。
bool visit[510];
const int inf = 99999999;
vector<int> path,temppath;
int minneed = inf,minback=inf,minn=inf;
void dfs(int v){ //一条路径顶到头才会去的dfs另一条路径。
if(v==0){
temppath.push_back(v);
int tneed=0,tback=0;
for(int i=temppath.size()-2;i>=0;i--){ //temppath是倒着push的,所以需要从后头开始
int id=temppath[i]; //路径第一个(size()-1)倒没什么,但i=0是最后那个点,必须要
if(weight[id]>0)
tback+=weight[id];
else{
if(tback>(0-weight[id]))
tback+=weight[id];
else{
tneed+=(0-weight[id]-tback);
tback=0;
}
}
}
if(tneed < minneed) {
minneed = tneed;
minback = tback;
path = temppath;
} else if(tneed == minneed && tback < minback) {
minback = tback;
path = temppath;
}
temppath.pop_back();
return ;
}
temppath.push_back(v);
for(int i=0;i<pre[v].size();i++)
dfs(pre[v][i]);
temppath.pop_back();
}
int main(){
fill(e[0], e[0] + 510 * 510, inf);
fill(dis, dis + 510, inf);
scanf("%d%d%d%d", &cmax, &n, &sp, &m);
for(int i=1;i<=n;i++){ //细节问题,n给的是除0以外的点,所以从1开始
cin>>weight[i];
weight[i]=weight[i]-cmax/2;
}
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
cin>>e[a][b];
e[b][a]=e[a][b];
}
dis[0]=0;
for(int i=0;i<=n;i++){ //n+1次循环
int u=-1;minn=inf;
for(int j=0;j<=n;j++)
if(visit[j]==false&&dis[j]<minn){
u=j;
minn=dis[j];
}
if(u==-1)
break;
visit[u]=true;
for(int v=0;v<=n;v++){
if(visit[v]==false&&e[u][v]!=inf){
if(dis[v]>dis[u]+e[u][v]){
dis[v]=dis[u]+e[u][v];
pre[v].clear();
pre[v].push_back(u);
}
else if(dis[v]==dis[u]+e[u][v])
pre[v].push_back(u);
}
}
}
dfs(sp);
printf("%d 0", minneed);
for(int i = path.size() - 2; i >= 0; i--)
printf("->%d", path[i]);
printf(" %d", minback);
return 0;
}