IntermediateLists & Dictionaries25 points

RFM scoring for a London fashion shop

An online fashion shop based in London wants to group customers with the classic RFM model: how Recently they bought, how Frequently, and how much Money they spent. Marketing sends a different email to each segment.

Task: line 1 is today (a day number). Line 2 is the number of customers n. Then n lines id last_day orders spend (id is text without spaces, the other three are integers). Recency in days is today - last_day. Each of the three metrics gets a score from 1 to 3 as follows. Sort all customers by the key below; if the customer is at 0-based position i, the score is i * 3 // n + 1.

  • R: sort by recency days descending, then id ascending (so the most recent buyers end last and score highest)
  • F: sort by orders ascending, then id ascending
  • M: sort by spend ascending, then id ascending

The total is R + F + M. Segment: total 8 or 9 is Champion, 5 to 7 is Loyal, 3 or 4 is AtRisk. Print one line per customer in the form id R F M total segment, sorted by total descending, then id ascending (plain string order).

Sample tests

Input

400
6
C01 395 12 5400
C02 300 2 150
C03 380 7 2300
C04 120 1 90
C05 390 5 980
C06 250 3 400

Expected output

C01 3 3 3 9 Champion
C03 2 3 3 8 Champion
C05 3 2 2 7 Loyal
C06 1 2 2 5 Loyal
C02 2 1 1 4 AtRisk
C04 1 1 1 3 AtRisk

Input

100
3
A 99 5 500
B 50 5 500
C 10 5 500

Expected output

C 1 3 3 7 Loyal
B 2 2 2 6 Loyal
A 3 1 1 5 Loyal

Your answer is also checked against 3 hidden tests.