#include<cstdio> #include<iostream> #include<algorithm> #include<vector> #include<tr1/unordered_map> #include<queue> #include<cstdlib> #include<list> #include<set> #include<map> #include<cmath> #define MP make_pair #define PB push_back #define s second #define f first #define PII pair<int,int> #define VPII vector <pair<int,int> > #define VI vector <int> #define abs(a) max((a),-(a)) #define LL long long #define LD long double #define ALL(x) x.begin(),x.end() #define PU putchar_unlocked #define GU getchar_unlocked #define DBG(x) cerr<<#x<<" "<<(x)<<endl; using namespace std; int a,b,c,d,e,f,n,m,mx,l,z,r,k; const int N=2e3+4; vector< pair< int, PII > > V; int poc[N]; int ILE; VI x; bool dodaj(int a,int b) { while(poc[a]!=0&&a<=b) { x.PB(a); // ILE++; if(poc[a]<=b)a=poc[a]+1; else { c=a; a=b+1; b=poc[c]; } } while(x.size()) { poc[x.back()]=max(poc[x.back()],b); x.pop_back(); } if(a>b)return 0; //printf("a %d %d\n",a,b); if(poc[b]!=0)poc[a]=poc[b]; poc[a]=b; return 1; } void solve() { scanf("%d",&n); for(int i=1;i<=n;i++) { for(int j=i;j<=n;j++) { scanf("%d",&a); V.PB(MP(a,MP(i,j))); } } sort(ALL(V)); reverse(ALL(V)); LL wynik=0; for(int j=1;j<=n;j++) { while(dodaj(V.back().s.f,V.back().s.s)==0) { V.pop_back(); } wynik+=V.back().f; // cout<<V.back().s.f<<" "<<V.back().s.s<<endl; V.pop_back(); } //cerr<<"x "<<ILE<<endl; printf("%lld\n",wynik); } main() { solve(); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | #include<cstdio> #include<iostream> #include<algorithm> #include<vector> #include<tr1/unordered_map> #include<queue> #include<cstdlib> #include<list> #include<set> #include<map> #include<cmath> #define MP make_pair #define PB push_back #define s second #define f first #define PII pair<int,int> #define VPII vector <pair<int,int> > #define VI vector <int> #define abs(a) max((a),-(a)) #define LL long long #define LD long double #define ALL(x) x.begin(),x.end() #define PU putchar_unlocked #define GU getchar_unlocked #define DBG(x) cerr<<#x<<" "<<(x)<<endl; using namespace std; int a,b,c,d,e,f,n,m,mx,l,z,r,k; const int N=2e3+4; vector< pair< int, PII > > V; int poc[N]; int ILE; VI x; bool dodaj(int a,int b) { while(poc[a]!=0&&a<=b) { x.PB(a); // ILE++; if(poc[a]<=b)a=poc[a]+1; else { c=a; a=b+1; b=poc[c]; } } while(x.size()) { poc[x.back()]=max(poc[x.back()],b); x.pop_back(); } if(a>b)return 0; //printf("a %d %d\n",a,b); if(poc[b]!=0)poc[a]=poc[b]; poc[a]=b; return 1; } void solve() { scanf("%d",&n); for(int i=1;i<=n;i++) { for(int j=i;j<=n;j++) { scanf("%d",&a); V.PB(MP(a,MP(i,j))); } } sort(ALL(V)); reverse(ALL(V)); LL wynik=0; for(int j=1;j<=n;j++) { while(dodaj(V.back().s.f,V.back().s.s)==0) { V.pop_back(); } wynik+=V.back().f; // cout<<V.back().s.f<<" "<<V.back().s.s<<endl; V.pop_back(); } //cerr<<"x "<<ILE<<endl; printf("%lld\n",wynik); } main() { solve(); } |