import java.lang.Math.*

open class Floor(val position:Int, val height:Int, deviceCount: Int, private val timeWhileBurnOut:Int){
    private var mFireSpreaded:Boolean = false
    var nextSchedule:FireEvent = NoEvent
        private set
    var restDevice:Int = deviceCount
        private set
    var isBurnedOut:Boolean = false
        private set
    open val hasNoDevice:Boolean
        get() = restDevice == 0
    open fun loadToElevator(deviceCount:Int):Unit {
        restDevice -= deviceCount
    }
    open fun fireSpreadAt(time:Double):Unit {
        if (!mFireSpreaded){
            mFireSpreaded = true
            nextSchedule = SpreadFire(this, time)
        }
    }
    open fun fireSpread():Unit {
        //println("Spread Fire at $this")
        nextSchedule = BurnOut(this, nextSchedule.time + timeWhileBurnOut)
    }
    open fun burnOut():Unit {
        //println("$this is BurnedOut")
        isBurnedOut = true
        nextSchedule = NoEvent
    }

    override fun toString(): String {
        return when {
            isBurnedOut -> "F(${position}, BurnedOut)"
            hasNoDevice -> "F(${position}, _NoDevice_)"
            else -> "F($position, D[$restDevice])"
        }
    }
    open val isGroundFloor:Boolean = false
}
object Ground:Floor(0, 0, 0, 0){
    override val hasNoDevice:Boolean
        get() = false
    override fun loadToElevator(deviceCount: Int) = Unit
    override fun fireSpreadAt(time: Double) = Unit
    override fun fireSpread() = Unit
    override fun burnOut() = Unit
    override fun toString(): String {
        return "GroundFloor"
    }

    override val isGroundFloor: Boolean = true
}
class Elevator(capacity:Int, velocity:Int, stopTime:Int, initPosition:Floor, val id:Int){
    private val mCapacity:Int = capacity
    private val mVelocity:Double = velocity.toDouble()
    private val mStopTime:Int = stopTime
    private var mStopPosition:Double = initPosition.height.toDouble()
    private var mLeaveTime:Double = 0e0
    var nextSchedule:ElevatorEvent = NoEvent
        private set
    val capacity:Int
        get() = mCapacity - loadedDevice
    var loadedDevice:Int = 0
        private set
    val isMovingToTop:Boolean
        get() = nextSchedule is ReachTop
    fun moveToLoad(floor:Floor):Unit {
        //println("$this move to $floor, capacity = ${this.capacity}")
        mLeaveTime = nextSchedule.time
        nextSchedule = reach(floor, this, nextSchedule.time + abs(floor.height - mStopPosition) / mVelocity)
    }
    fun changeTargetFloorAt(floor:Floor, time:Double):Unit {
        //println("Elevator[$id] change direction from [${(nextSchedule as ReachTop).floor}] to [${floor}]")
        val prevSchedule = nextSchedule as ReachTop
        mStopPosition += (time - mLeaveTime) / (prevSchedule.time - mLeaveTime) * (prevSchedule.floor.height - mStopPosition)
        mLeaveTime = time
        nextSchedule = reach(floor, this, time + abs(floor.height - mStopPosition) / mVelocity)
    }
    fun moveToUnload():Unit {
        //println("Elevator[$this] move to Ground. loaded = ${this.loadedDevice}")
        mLeaveTime = nextSchedule.time
        nextSchedule = ReachGround(this, nextSchedule.time + mStopPosition / mVelocity)
    }
    fun loadDevice(deviceCount:Int):Unit {
        //println("E$id loadDevice $deviceCount")
        mStopPosition = (nextSchedule as ReachTop).floor.height.toDouble()
        when(deviceCount){
            0 -> nextSchedule = NoEvent
            else -> {
                loadedDevice += deviceCount
                nextSchedule = LoadEnd(this, nextSchedule.time + mStopTime)
            }
        }
    }
    fun unloadDevice():Unit {
        //println("E$id unloadDevice")
        mStopPosition = 0e0
        loadedDevice = 0
        nextSchedule = UnloadEnd(this, nextSchedule.time + mStopTime)
    }
    fun elevatorShutDown():Unit {
        //println("$this is ShutDown")
        nextSchedule = NoEvent
    }
    override fun toString(): String {
        val next = nextSchedule
        return when (next){
            is ReachTop -> "E([$id] Move To ${next.floor})"
            is ReachGround -> "E([$id] To Ground, D[$loadedDevice])"
            is LoadEnd -> "E([$id] Loading)"
            is UnloadEnd -> "E([$id] Unloading)"
            else -> "E([$id] is Offline)"
        }
    }
}
fun reach(floor: Floor, elevator: Elevator, time:Double):ElevatorEvent{
    return when{
        floor.isGroundFloor -> ReachGround(elevator, time)
        else -> ReachTop(floor, elevator, time)
    }
}
interface Event{
    val time:Double
    val hasSchedule:Boolean
        get() = true
}
interface FireEvent:Event
interface ElevatorEvent:Event
interface BuildingEvent:Event
class BurnOut(val floor:Floor, override val time:Double):FireEvent, BuildingEvent{
    override fun toString(): String {
        return "BurnOut $floor, at $time"
    }
}
class SpreadFire(val floor:Floor, override val time:Double):FireEvent {
    override fun toString(): String {
        return "SpreadFire $floor, at $time"
    }
}
class ReachTop(val floor:Floor, val elevator: Elevator, override val time:Double):ElevatorEvent, BuildingEvent{
    override fun toString(): String {
        return "ReachTop $elevator at $floor, at $time"
    }
}
class LoadEnd(val elevator:Elevator, override val time:Double):ElevatorEvent, BuildingEvent {
    override fun toString(): String {
        return "LoadEnd $elevator, at $time"
    }
}
class UnloadEnd(val elevator: Elevator, override val time:Double):ElevatorEvent, BuildingEvent{
    override fun toString(): String {
        return "UnloadEnd $elevator, at $time"
    }
}
class ReachGround(val elevator:Elevator, override val time:Double):ElevatorEvent, BuildingEvent {
    override fun toString(): String {
        return "ReachGround $elevator, at $time"
    }
}
object NoEvent:FireEvent, ElevatorEvent, BuildingEvent{
    override fun toString(): String {
        return "NoEvent"
    }
    override val time:Double = 0e0
    override val hasSchedule: Boolean
        get() = false
}

class Building private constructor(private val mFloors:Array<Floor>, private val mElevators:Iterable<Elevator>, private val timeWhileSpreadUp:Int, private val timeWhileSpreadDown:Int, deviceAlreadyInFirstFloor:Int){
    override fun toString(): String {
        return "Building [$deviceInGround] " + mFloors.joinToString(" ") + "\n" + mElevators.joinToString(" ") + "\n" + topFloor
    }
    private fun mostRecentlyEvent():Event {
        return listOf<Iterable<Event>>(mFloors.map { it.nextSchedule }.filter{it.hasSchedule}, mElevators.map{it.nextSchedule}.filter{it.hasSchedule}).flatten().minBy{it.time} ?: NoEvent
    }
    var nextSchedule:BuildingEvent = NoEvent
        private set
    var topFloor:Floor = mFloors.findLast { !it.isBurnedOut && !it.hasNoDevice }!!
        private set
    val elevators:Iterable<Elevator> = mElevators
    var deviceInGround:Int = deviceAlreadyInFirstFloor
    fun eventExhaustion():Unit {
        loop@while (true){
            val event = mostRecentlyEvent()
            when(event){
                is BuildingEvent -> {
                    nextSchedule = event
                    break@loop
                }
                is SpreadFire -> {
                    event.floor.fireSpread()
                    if (mFloors.indices.contains(event.floor.position))
                        mFloors[event.floor.position].fireSpreadAt(event.time + timeWhileSpreadUp)
                    if (mFloors.indices.contains(event.floor.position - 2))
                        mFloors[event.floor.position - 2].fireSpreadAt(event.time + timeWhileSpreadDown)
                }
            }
        }
    }
    fun renewTopFloor():Unit{
        topFloor = mFloors.findLast { !it.isBurnedOut && !it.hasNoDevice }!!
    }
    companion object {
        fun read(m:Int, line:() -> String):Building {
            val d = line().toInt()
            val floorPram = line().split(' ').map(String::toInt)
            val elevatorParam = Array(m){line().split(' ').map(String::toInt)}
            val (k, tx, ty, tz) = line().split(' ').map(String::toInt)
            val floor = floorPram.mapIndexed{ i, n -> Floor(i + 1, i * d, n, tx)}.toTypedArray()
            floor[0] = Ground
            floor[k - 1].fireSpreadAt(0e0)
            val elevator = elevatorParam.mapIndexed{index, l ->
                val (c, v, t, x) = l
                Elevator(c, v, t, floor[x - 1], index + 1)
            }
            val res = Building(floor, elevator, ty, tz, floorPram.first())
            res.elevators.forEach { it.moveToLoad(res.topFloor) }
            res.eventExhaustion()
            return res
        }
    }
}
fun min(a:Int, b:Int):Int = if (a > b) b else a
fun solve(building:Building):Unit {
    var lastUnloadEndTime:Double = 0e0
    with(building){
        loop@while (true){
            val event = nextSchedule
            //println("\n" + event)
            //println(building)
            when(event){
                is BurnOut -> {
                    event.floor.burnOut()
                    if (topFloor.isBurnedOut) {
                        renewTopFloor()
                        elevators
                                .filter { it.isMovingToTop }
                                .forEach { it.changeTargetFloorAt(topFloor, event.time) }
                    }
                }
                is ReachTop -> {
                    val canLoadDevice = min(event.elevator.capacity, event.floor.restDevice)
                    event.elevator.loadDevice(canLoadDevice)
                    event.floor.loadToElevator(canLoadDevice)
                    if (event.floor.hasNoDevice) {
                        renewTopFloor()
                        elevators.filter { it.isMovingToTop }
                                .forEach { it.changeTargetFloorAt(topFloor, event.time) }
                    }
                }
                is ReachGround -> {
                    if (event.elevator.loadedDevice == 0){
                        event.elevator.elevatorShutDown()
                    }else {
                        deviceInGround += event.elevator.loadedDevice
                        event.elevator.unloadDevice()
                    }
                }
                is LoadEnd -> {
                    if (event.elevator.capacity != 0) event.elevator.moveToLoad(topFloor)
                    else event.elevator.moveToUnload()
                }
                is UnloadEnd -> {
                    if (topFloor.isGroundFloor) {
                        event.elevator.elevatorShutDown()
                    } else {
                        event.elevator.moveToLoad(topFloor)
                    }
                    lastUnloadEndTime = event.time
                }
                is NoEvent -> break@loop
            }
            eventExhaustion()
        }
    }
    println("${building.deviceInGround} $lastUnloadEndTime")
}
fun main(args:Array<String>):Unit {
    var (n, m) = readLine()!!.split(' ').map(String::toInt)
    while (n != 0 || m != 0) {
        solve(Building.read(m) { readLine()!! })
        val (nn, mm) = readLine()!!.split(' ').map(String::toInt)
        n = nn
        m = mm
    }
}


Measured memory_bytes: 85584