algorithms

Algorithm implementations
git clone git://git.laack.co/algorithms.git
Log | Files | Refs | README

main.cpp (1307B)


      1 #include <iostream>
      2 #include <vector>
      3 #include <unordered_set>
      4 #include <unordered_map>
      5 #include <algorithm>
      6 
      7 #define ll long long
      8 
      9 using namespace std;
     10 
     11 // - can we just combine the central elements as much as possible?
     12 //     - that seems right.
     13 //         - combine central elements together until no more, than split largest into 1 and other
     14 
     15 int main() {
     16     int testCases, sz, current;
     17     cin >> testCases;
     18     for(int m = 0; m < testCases; ++m) {
     19         cin >> sz;
     20 
     21         vector<int> ls {};
     22 
     23         for(int i = 0; i < sz; ++i) {
     24             cin >> current;
     25             ls.push_back(current);
     26         }
     27 
     28         int innerSum = 0;
     29 
     30         bool allOnes = true;
     31 
     32         for(int i = 0; i < sz; ++i) {
     33             if(ls[i] != 1) {
     34                 allOnes = false;
     35                 break;
     36             }
     37         }
     38 
     39         if(allOnes) {
     40             cout << -1 << endl;
     41             continue;
     42         }
     43 
     44         bool ib = false;
     45 
     46         for(int i = 1; i < sz-1; ++i) {
     47             innerSum += ls[i];
     48             if(ls[i] >= 2) {
     49                 ib = true;
     50             }
     51         }
     52 
     53         if(!ib) {
     54             innerSum = 0;
     55         }
     56 
     57         int left = ls[0];
     58         int right = ls[ls.size() - 1];
     59         
     60         left += innerSum;
     61         right += left - 1;
     62         cout << right << endl;
     63 
     64     }
     65 }