// O(n^2) のTLE解を unordered_map にして、定数倍高速化したやつ #include using namespace std; int solve() { int n; cin >> n; vector A(n); for (int i = 0; i < n; i++) cin >> A[i]; unordered_map q1, q2; auto &cur = q1; auto &nex = q2; cur[0] = 0; for (int i = 0; i < n; i++) { for (auto cl : cur) { int c = cl.first; int l = cl.second; if (l < A[i]) { if (nex.count(c) == 0 || nex[c] > A[i]) { nex[c] = A[i]; } } if (nex.count(c + 1) == 0 || nex[c + 1] > l + 1) { nex[c + 1] = l + 1; } } swap(cur, nex); nex.clear(); } int ans = 1e9; for (auto cl : cur) { ans = min(ans, cl.first); } return ans; //return cur.begin()->fst; // mapならこちら } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << solve() << endl; return 0; }