#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
int n,m,i,i2,a,b,c;
char s;
vector<int>v,v2,v1,v4,vd;
//v1 la stack muitipurpuse
//v la working time
//v4 la longest last node -1 if none -2 if more than 2
//vd distance
//v2 la requerment
vector<vector<array<int,2>>>v3;
while(cin>>n>>m){
v.resize(1);
v2.resize(0);
v3.resize(0);
v4.resize(0);
vd.resize(0);
v2.resize(n,0);
v3.resize(n);
v4.resize(n,-1);
vd.resize(n,0);
cin>>v[0];
for(i=n;--i;)cin>>s>>i2,v.push_back(i2);
while(m--){
cin>>a>>b>>c;
v2[b]++;
v3[a].push_back({b,c});
}
for(i=n;i--;){
for(v1.push_back(i);!v1.empty();){
i2=v1.back();
v1.pop_back();
if(!v2[i2]--){//neu cac node truoc da xong
vd[i2]+=v[i2];
for(auto i:v3[i2]){
if(vd[i[0]]<(i[1]+=vd[i2])){
v4[i[0]]=(v4[i2]==-2?-2:i2);
vd[i[0]]=i[1];
}else if(vd[i[0]]==i[1])v4[i[0]]=-2;
v1.push_back(i[0]);
}
}
}
}
i=max_element(vd.begin(),vd.end())-vd.begin();
cout<<vd[i];
if(v4[i]!=-2 and count(vd.begin(),vd.end(),vd[i])){
for(v1.push_back(i);v1.back()!=-1;v1.push_back(v4[v1.back()]));
v1.pop_back();
while(!v1.empty()){
cout<<","<<v1.back();
v1.pop_back();
}
cout<<"\n";
}else cout<<",M\n";
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IG1haW4oKXsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCksY2luLnRpZSgwKTsKICAgIGludCBuLG0saSxpMixhLGIsYzsKICAgIGNoYXIgczsKICAgIHZlY3RvcjxpbnQ+dix2Mix2MSx2NCx2ZDsKICAgIC8vdjEgbGEgc3RhY2sgbXVpdGlwdXJwdXNlCiAgICAvL3YgbGEgd29ya2luZyB0aW1lCiAgICAvL3Y0IGxhIGxvbmdlc3QgbGFzdCBub2RlIC0xIGlmIG5vbmUgLTIgaWYgbW9yZSB0aGFuIDIKICAgIC8vdmQgZGlzdGFuY2UKICAgIC8vdjIgbGEgcmVxdWVybWVudAogICAgdmVjdG9yPHZlY3RvcjxhcnJheTxpbnQsMj4+PnYzOwogICAgd2hpbGUoY2luPj5uPj5tKXsKICAgICAgICB2LnJlc2l6ZSgxKTsKICAgICAgICB2Mi5yZXNpemUoMCk7CiAgICAgICAgdjMucmVzaXplKDApOwogICAgICAgIHY0LnJlc2l6ZSgwKTsKICAgICAgICB2ZC5yZXNpemUoMCk7CiAgICAgICAgdjIucmVzaXplKG4sMCk7CiAgICAgICAgdjMucmVzaXplKG4pOwogICAgICAgIHY0LnJlc2l6ZShuLC0xKTsKICAgICAgICB2ZC5yZXNpemUobiwwKTsKICAgICAgICBjaW4+PnZbMF07CiAgICAgICAgZm9yKGk9bjstLWk7KWNpbj4+cz4+aTIsdi5wdXNoX2JhY2soaTIpOwogICAgICAgIHdoaWxlKG0tLSl7CiAgICAgICAgICAgIGNpbj4+YT4+Yj4+YzsKICAgICAgICAgICAgdjJbYl0rKzsKICAgICAgICAgICAgdjNbYV0ucHVzaF9iYWNrKHtiLGN9KTsKICAgICAgICB9CiAgICAgICAgZm9yKGk9bjtpLS07KXsKICAgICAgICAgICAgZm9yKHYxLnB1c2hfYmFjayhpKTshdjEuZW1wdHkoKTspewogICAgICAgICAgICAgICAgaTI9djEuYmFjaygpOwogICAgICAgICAgICAgICAgdjEucG9wX2JhY2soKTsKICAgICAgICAgICAgICAgIGlmKCF2MltpMl0tLSl7Ly9uZXUgY2FjIG5vZGUgdHJ1b2MgZGEgeG9uZwogICAgICAgICAgICAgICAgICAgIHZkW2kyXSs9dltpMl07CiAgICAgICAgICAgICAgICAgICAgZm9yKGF1dG8gaTp2M1tpMl0pewogICAgICAgICAgICAgICAgICAgICAgICBpZih2ZFtpWzBdXTwoaVsxXSs9dmRbaTJdKSl7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB2NFtpWzBdXT0odjRbaTJdPT0tMj8tMjppMik7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB2ZFtpWzBdXT1pWzFdOwogICAgICAgICAgICAgICAgICAgICAgICB9ZWxzZSBpZih2ZFtpWzBdXT09aVsxXSl2NFtpWzBdXT0tMjsKICAgICAgICAgICAgICAgICAgICAgICAgdjEucHVzaF9iYWNrKGlbMF0pOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpPW1heF9lbGVtZW50KHZkLmJlZ2luKCksdmQuZW5kKCkpLXZkLmJlZ2luKCk7CiAgICAgICAgY291dDw8dmRbaV07CiAgICAgICAgaWYodjRbaV0hPS0yIGFuZCBjb3VudCh2ZC5iZWdpbigpLHZkLmVuZCgpLHZkW2ldKSl7CiAgICAgICAgICAgIGZvcih2MS5wdXNoX2JhY2soaSk7djEuYmFjaygpIT0tMTt2MS5wdXNoX2JhY2sodjRbdjEuYmFjaygpXSkpOwogICAgICAgICAgICB2MS5wb3BfYmFjaygpOwogICAgICAgICAgICB3aGlsZSghdjEuZW1wdHkoKSl7CiAgICAgICAgICAgICAgICBjb3V0PDwiLCI8PHYxLmJhY2soKTsKICAgICAgICAgICAgICAgIHYxLnBvcF9iYWNrKCk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY291dDw8IlxuIjsKICAgICAgICB9ZWxzZSBjb3V0PDwiLE1cbiI7CiAgICB9Cn0K
NiA3CjEwICw4ICw5ICwxMCAsMTEgLDEyCjAgMSAxCjEgMiAyCjEgMyAzCjEgNCA0CjIgNSA1CjMgNSA2CjQgNSA3CjYgNwoxMCAsOCAsOSAsMTEgLDExICwxMgowIDEgMQoxIDIgMgoxIDMgNAoxIDQgNAoyIDUgNQozIDUgNwo0IDUgNwo4IDExCjIgLDcgLDIgLDYgLDUgLDEgLDIgLDcKNiA3IDIKMCAxIDQKMCAyIDIKMSAzIDYKMSA0IDUKMSA1IDMKMiA0IDEKMyA2IDIKMyA3IDkKNCA1IDIKNSA3IDI=
6 7
10 ,8 ,9 ,10 ,11 ,12
0 1 1
1 2 2
1 3 3
1 4 4
2 5 5
3 5 6
4 5 7
6 7
10 ,8 ,9 ,11 ,11 ,12
0 1 1
1 2 2
1 3 4
1 4 4
2 5 5
3 5 7
4 5 7
8 11
2 ,7 ,2 ,6 ,5 ,1 ,2 ,7
6 7 2
0 1 4
0 2 2
1 3 6
1 4 5
1 5 3
2 4 1
3 6 2
3 7 9
4 5 2
5 7 2