2606.cpp (507B)
1 class Solution { 2 public: 3 int maximumCostSubstring(const string &s, const string &chars, const vector<int> &vals) { 4 static int cost[27]; 5 6 for (int i = 1; i <= 26; i++) 7 cost[i] = i; 8 for (int i = 0; i < size(vals); i++) 9 cost[chars[i] & 0x1F] = vals[i]; 10 11 int res = 0, crnt = 0; 12 for (int i = 0; i < size(s); i++) { 13 crnt = max(0, crnt + cost[s[i] & 0x1F]); 14 res = max(res, crnt); 15 } 16 17 return res; 18 } 19 };