#include <cmath>
#include <map>
#include<fstream>
#include<algorithm>
#include <iostream>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include <iomanip>
#include<assert.h>
#include<memory.h>
#include<bitset>
#include<algorithm>
#include <cassert>
#include <stack>
#include<cstring>
#include <cmath>
#include <complex>
#include <sstream>
#include<cstring>
#include <complex>
#include<cstdlib>
#include <set>
#include <ctime>
#include <queue>
#include <vector>
#include <iterator>
#include <math.h>
#include <cstdio>
#include<map>
using namespace std;
#define ll long long
#define MAXN 110005
#define fast std::ios_base::sync_with_stdio(false);
#define sz(x) ((int)(x.size()))
#define REP(i,v) for(int i = 0 ; i < sz(v) ; ++i)
vector<int> v;
int DP[100001];
int main()
{
int n;
scanf("%d",&n);
v.resize(n);
for(int i = 0; i < n ; ++i)
{
scanf("%d",&v[i]); --v[i];
DP[v[i]] = i;
}
for(int i = n - 2 ; i >= 0 ; --i)
{
DP[i] = max(DP[i],DP[i + 1]);
}
int ans = -1;
for(int i = 0 ; i < n ; ++i)
{
if(v[i] != n - 1 && DP[v[i]] - i > 0)
ans = max(ans,DP[v[i]] - i);
}
cout << ans;
}
I2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZTxmc3RyZWFtPgojaW5jbHVkZTxhbGdvcml0aG0+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGU8bnVtZXJpYz4KI2luY2x1ZGU8dXRpbGl0eT4KI2luY2x1ZGU8ZnVuY3Rpb25hbD4KI2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGUgPGlvbWFuaXA+CiNpbmNsdWRlPGFzc2VydC5oPgojaW5jbHVkZTxtZW1vcnkuaD4KI2luY2x1ZGU8Yml0c2V0PgojaW5jbHVkZTxhbGdvcml0aG0+CiNpbmNsdWRlIDxjYXNzZXJ0PgojaW5jbHVkZSA8c3RhY2s+CiNpbmNsdWRlPGNzdHJpbmc+CiNpbmNsdWRlIDxjbWF0aD4KI2luY2x1ZG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error: illegal character: '#'
#include <cmath>
^
Main.java:1: error: class, interface, or enum expected
#include <cmath>
^
Main.java:2: error: illegal character: '#'
#include <map>
^
Main.java:3: error: illegal character: '#'
#include<fstream>
^
Main.java:4: error: illegal character: '#'
#include<algorithm>
^
Main.java:5: error: illegal character: '#'
#include <iostream>
^
Main.java:6: error: illegal character: '#'
#include<numeric>
^
Main.java:7: error: illegal character: '#'
#include<utility>
^
Main.java:8: error: illegal character: '#'
#include<functional>
^
Main.java:9: error: illegal character: '#'
#include<stdio.h>
^
Main.java:10: error: illegal character: '#'
#include <iomanip>
^
Main.java:11: error: illegal character: '#'
#include<assert.h>
^
Main.java:12: error: illegal character: '#'
#include<memory.h>
^
Main.java:13: error: illegal character: '#'
#include<bitset>
^
Main.java:14: error: illegal character: '#'
#include<algorithm>
^
Main.java:15: error: illegal character: '#'
#include <cassert>
^
Main.java:16: error: illegal character: '#'
#include <stack>
^
Main.java:17: error: illegal character: '#'
#include<cstring>
^
Main.java:18: error: illegal character: '#'
#include <cmath>
^
Main.java:19: error: illegal character: '#'
#include <complex>
^
Main.java:20: error: illegal character: '#'
#include <sstream>
^
Main.java:21: error: illegal character: '#'
#include<cstring>
^
Main.java:22: error: illegal character: '#'
#include <complex>
^
Main.java:23: error: illegal character: '#'
#include<cstdlib>
^
Main.java:24: error: illegal character: '#'
#include <set>
^
Main.java:25: error: illegal character: '#'
#include <ctime>
^
Main.java:26: error: illegal character: '#'
#include <queue>
^
Main.java:27: error: illegal character: '#'
#include <vector>
^
Main.java:28: error: illegal character: '#'
#include <iterator>
^
Main.java:29: error: illegal character: '#'
#include <math.h>
^
Main.java:30: error: illegal character: '#'
#include <cstdio>
^
Main.java:31: error: illegal character: '#'
#include<map>
^
Main.java:33: error: illegal character: '#'
#define ll long long
^
Main.java:33: error: class, interface, or enum expected
#define ll long long
^
Main.java:34: error: illegal character: '#'
#define MAXN 110005
^
Main.java:35: error: illegal character: '#'
#define fast std::ios_base::sync_with_stdio(false);
^
Main.java:36: error: illegal character: '#'
#define sz(x) ((int)(x.size()))
^
Main.java:36: error: class, interface, or enum expected
#define sz(x) ((int)(x.size()))
^
Main.java:37: error: illegal character: '#'
#define REP(i,v) for(int i = 0 ; i < sz(v) ; ++i)
^
Main.java:37: error: class, interface, or enum expected
#define REP(i,v) for(int i = 0 ; i < sz(v) ; ++i)
^
Main.java:37: error: class, interface, or enum expected
#define REP(i,v) for(int i = 0 ; i < sz(v) ; ++i)
^
Main.java:39: error: class, interface, or enum expected
int DP[100001];
^
Main.java:40: error: class, interface, or enum expected
int main()
^
Main.java:44: error: class, interface, or enum expected
scanf("%d",&n);
^
Main.java:45: error: class, interface, or enum expected
v.resize(n);
^
Main.java:46: error: class, interface, or enum expected
for(int i = 0; i < n ; ++i)
^
Main.java:46: error: class, interface, or enum expected
for(int i = 0; i < n ; ++i)
^
Main.java:46: error: class, interface, or enum expected
for(int i = 0; i < n ; ++i)
^
Main.java:48: error: class, interface, or enum expected
scanf("%d",&v[i]); --v[i];
^
Main.java:49: error: class, interface, or enum expected
DP[v[i]] = i;
^
Main.java:50: error: class, interface, or enum expected
}
^
Main.java:51: error: class, interface, or enum expected
for(int i = n - 2 ; i >= 0 ; --i)
^
Main.java:51: error: class, interface, or enum expected
for(int i = n - 2 ; i >= 0 ; --i)
^
Main.java:54: error: class, interface, or enum expected
}
^
Main.java:56: error: class, interface, or enum expected
for(int i = 0 ; i < n ; ++i)
^
Main.java:56: error: class, interface, or enum expected
for(int i = 0 ; i < n ; ++i)
^
Main.java:56: error: class, interface, or enum expected
for(int i = 0 ; i < n ; ++i)
^
Main.java:61: error: class, interface, or enum expected
}
^
Main.java:64: error: class, interface, or enum expected
}
^
59 errors