#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, dic, r;
    cin >> n >> dic >> r;
    vector<int> a(n + 1);
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    int ans = n + 1;
    vector<int> p(r + 1);
    vector<int> k(r + 1);
    for(int i = 1; i <= r; i++) {
        cin >> p[i] >> k[i];
    }
    vector<int> pos[n + 1];
    vector<int> some(n + 1, 1e9);
    for(int i = 1; i <= n; i++) {
        pos[a[i]].push_back(i);
    }
    for(int i = 1; i <= r; i++) {
        some[p[i]] = pos[p[i]].size() - 1;
    }
    vector<int> c(n + 1, n + 1);
    for(int i = n; i >= 1; i--) {
        c[a[i]] = some[a[i]]--;
        int ok = 1;
        int d = 0;
        for(int j = 1; j <= r; j++) {
            if(k[j] + c[p[j]] - 1 >= pos[p[j]].size()) {
                ok = 0;
                break;
            }
            d = max(d, pos[p[j]][k[j] + c[p[j]] - 1]);
        }
        if(!ok) {
            continue;
        }
        ans = min(ans, d - i + 1);
    }
    if(ans == n + 1) {
        cout << "impossible";
    }
    else {
        cout << ans;
    }
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |