fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27. // 7
  28. // 5 8 8 9 11 12 13
  29.  
  30.  
  31. vector<int> a;
  32.  
  33. int consistency1(int n){
  34.  
  35. int ans = a[0];
  36.  
  37. for(int i=1; i<n; i++){
  38.  
  39. int sum = a[i];
  40. ans += a[i];
  41. int j = i-1;
  42.  
  43. int dif = a[j+1] - a[j];
  44.  
  45. if(dif != 0 && dif != 1 ) continue;
  46.  
  47. while(j>=0 && a[j+1] - a[j] == dif){
  48.  
  49. sum += a[j];
  50. ans += sum;
  51. j--;
  52. }
  53. }
  54.  
  55. return ans;
  56. }
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. int consistency2(int n){
  65.  
  66.  
  67. int ans1 = 0;
  68.  
  69. int last = 0;
  70. int len = 0;
  71.  
  72. for(int i=0; i<n; i++){
  73.  
  74. if(i-1>=0 && a[i] - a[i-1] == 1){
  75. len++;
  76. last = len*a[i] + last;
  77. ans1 += last;
  78.  
  79. }
  80. else{
  81. len = 1;
  82. last = a[i];
  83. ans1 += last;
  84. }
  85. }
  86.  
  87. int ans2 = 0;
  88. last = 0;
  89. len = 0;
  90.  
  91. for(int i=0; i<n; i++){
  92.  
  93. if(i-1>=0 && a[i] - a[i-1] == 0){
  94. len++;
  95. last = len*a[i] + last;
  96. ans2 += last;
  97. }
  98. else{
  99. len = 1;
  100. last = a[i];
  101. ans2 += last;
  102. }
  103. }
  104.  
  105.  
  106. int ans = ans1 + ans2;
  107.  
  108. for(int i=0; i<n; i++) ans -= a[i];
  109.  
  110.  
  111.  
  112. return ans;
  113. }
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. int practice(int n){
  125.  
  126.  
  127. return 0;
  128. }
  129.  
  130.  
  131.  
  132.  
  133.  
  134. void solve() {
  135.  
  136. int n;
  137. cin>> n;
  138.  
  139. a.resize(n);
  140. for(int i=0; i<n; i++) cin >> a[i];
  141.  
  142. cout << consistency1(n) << " " << consistency2(n) << endl;
  143.  
  144.  
  145. }
  146.  
  147.  
  148.  
  149.  
  150.  
  151. int32_t main() {
  152. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  153.  
  154. int t = 1;
  155. // cin >> t;
  156. while (t--) {
  157. solve();
  158. }
  159.  
  160. return 0;
  161. }
Success #stdin #stdout 0.01s 5320KB
stdin
7
5 8 8 9 11 12 13 
stdout
183 183