Imagine you’ve arrived at the annual NTHU Career & Recruitment Fair, where along a long corridor there are N drink booths arranged in a line from the entrance to the exit. To reflect differences in foot traffic and location value:
Booths at the two ends (near the entrance and exit) see the most people and therefore charge higher prices.
Mid-corridor booths have more dispersed traffic and tend to be cheaper.
Every drink type appears at least three times, and no two booths selling the same drink ever charge the same price.
You want to quantify each drink’s popularity score based on the sequence of its prices as you walk by.
If a drink appears at booths in positions 1 ≤ i1 < i2 < ⋯ < im ≤ N with corresponding prices p1 , p2 , … , pm then its popularity score is defined as max(pu - pv + pw) , for all u < v < w .
Intuitively, you pick three visits in chronological order—an early booth at price pu, a middle booth at pv, and a later booth at pw—and compute pu - pv + pw. Because end booths are pricier (more foot traffic) and middle booths are cheaper (less traffic), this formula captures “high-end interest” at both ends minus the mid-corridor bargain.
Note: This scoring formula does not imply that mid-corridor booths are always cheaper than end booths; it is simply one way to capture the overall trend in a drink’s popularity.
The first line contains a single integer N, the total number of drink booths arranged in a line at the NTHU Career & Recruitment Fair.
The second line contains N space-separated strings s1,s2,…,sN , where sis_isi is the name of the drink sold at booth i.
The third line contains N space-separated integers a1,a2,…,aN , where aia_iai is the price charged by booth i.
The fourth line contains an integer Q, the number of queries you will make, and then q more lines each containing a single string Tj ; each Tj is guaranteed to match at least one of the si.
1 ≤ N,Q ≤ 2 × 105
1 ≤ | Si |,| Tj | ≤ 10
1 ≤ ai ≤ 108
Testcases 1 ~ 3: Si ,Tj = "a"
Testcases 4 ~ 6: N,Q ≤ 1000
Testcases 7 ~ 10: No additional restrictions
map<string, vector<int>>
to collect, for each drink S, the list of its prices in the exact order they appear. That way each query can immediately grab the price‐sequence for its drink.map<string,int> ans
so you only do the above scan once per drink.For each of the Q queried drink names Tj, output on its own line the popularity score of that drink: the maximum value of pu - pv + pw over all triples of distinct occurrences u < v < w in the sequence of prices for drink Tj.