process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("\n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// function main() { var n = parseInt(readLine()); genes = readLine().split(' '); health = readLine().split(' '); health = health.map(Number); var s = parseInt(readLine()); var min = Number.MAX_SAFE_INTEGER var max = 0 for(var a0 = 0; a0 < s; a0++){ var first_temp = readLine().split(' '); var first = parseInt(first_temp[0]); var last = parseInt(first_temp[1]); var t = first_temp[2]; let temp = 0 // your code goes here for(let i = first;i<=last;i++) { let d = t let g = genes[i] //console.log('[go]',i) while ( d.indexOf( g ) >= 0 ) { temp += health[ i ] // console.log('[before]',g,d) d = d.slice(0, d.indexOf( g ) ) + d.slice( d.indexOf( g ) + 1 ) //console.log('[after]',d) } } if ( temp > max ) max = temp if ( temp < min ) min = temp } console.log( min + ' ' + max ) }