#include using namespace std; #include using namespace atcoder; using ll = int64_t; using ul = uint64_t; using ld = long double; using vi = vector; using vd = vector; using vc = vector; using vs = vector; using vb = vector; using vl = vector; using vvi = vector; using vvd = vector; using vvc = vector; using vvb = vector; using vvl = vector; using mint = modint998244353; using vm = vector; int main() { int N,Q; cin >> N >> Q; vl S(N); for (int i = 0; i < N; i++) { cin >> S[i]; } vvl lea(N); for (int i = 0; i < N; i++) { lea[i].push_back(S[i]); sort(lea[i].begin(), lea[i].end()); if (lea[i].size() > 20) lea[i].pop_back(); if (i + 1 < N) lea[i + 1] = lea[i]; } for (int i = 0; i < Q; i++) { int L,R,K; cin >> L >> R >> K; L--,R--; int pos = 0; ll ans = 0; for (auto s : lea[R]) { if (L == 0 || pos >= lea[L - 1].size() || lea[L - 1][pos] != s) { ans += s; K--; if (K == 0) break; } else { pos++; } } cout << ans << endl; } return 0; }