PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BF0000040. Lập lịch công việc có phụ thuộc (Schedule Problem)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Lập lịch công việc có phụ thuộc (Schedule Problem)

Schedule Problem

Source: HDU

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn tasks with continuous durations did_i. Relations are FAS a b (finish of aa no earlier than start of bb), FAF a b (finish of aa no earlier than finish of bb), SAF a b (start of aa no earlier than finish of bb), SAS a b (start of aa no earlier than start of bb). Tasks may overlap. Print a schedule minimizing project completion time, or impossible if no schedule exists.

The finish time of task ii is Ti+diT_i+d_i; the project completion time is max⁡i(Ti+di)\max_i(T_i+d_i). The minimum start time must be 00.

This Extended version uses explicit n,mn,m and no terminating sentinel.

Input

First line n,mn,m. Next are nn durations did_i, followed by mm lines TYPE a b, with TYPE one of FAS, FAF, SAF, SAS.

Output

Print impossible if infeasible; otherwise print nn lines i T_i in increasing task index, with nonnegative starts TiT_i.

Subtasks

  • Subtask 1 (20 points): $2\le n\le12; 0\le m\le30; 1\le d_i\le100; 1\le a,b\le n; a\ne b$.
  • Subtask 2 (30 points): $2\le n\le70; 0\le m\le400; 1\le d_i\le1000; 1\le a,b\le n; a\ne b$.
  • Subtask 3 (50 points): $2\le n\le300; 0\le m\le2500; 1\le d_i\le100000; 1\le a,b\le n; a\ne b$.

Examples

Input

3 2
2 3 4
SAF 2 1
FAF 3 2

Output

1 0
2 2
3 1

Explanation

SAF 2 1 gives T2≥T1+2T_2\ge T_1+2. FAF 3 2 yields T3+4≥T2+3T_3+4\ge T_2+3, or T3≥T2−1T_3\ge T_2-1. The starts (0,2,1)(0,2,1) satisfy both and finish the project in 55 time units.