longmaximumSum(vector<long> a, long mod){ long n = a.size(), res = 0; set<long> s; for(long i = 0, sum = 0; i < n; i++) { sum = (sum + a[i]) % mod; auto ub = s.upper_bound(sum); if(ub == end(s)) { res = max(res, sum); } else { res = max(res, mod - (*ub - sum)); } s.insert(sum); } return res; }