class Solution(object):
    def canCompleteCircuit(self, gas, cost):
        """
        :type gas: List[int]
        :type cost: List[int]
        :rtype: int
        """
        # get the number of the gas stations
        n = len(gas)
        # try every gas station
        for i in xrange(n):
            gasInTank = 0
            count = 0
            j = i
            while gasInTank >= 0 and gas[j%n] + gasInTank >= cost[j%n] and count < n:
                gasInTank += gas[j%n] - cost[j%n]
                j += 1
                count += 1
            if count == n:
                return i
        return -1

@@ -177,44 +177,35 @@
     
-# get the number of the gas stations
+total = 0%0A        start = 0
 %0A   
@@ -234,31 +234,29 @@
     
-# try every gas station
+subsum = float('inf')
 %0A   
@@ -296,197 +296,64 @@
     
-gasInTank = 0%0A            count = 0%0A            j = i%0A            while gasInTank %3E= 0 and gas%5Bj%25n%5D + gasInTank %3E= cost%5Bj%25n%5D and count %3C n:%0A                gasInTank += gas%5Bj%25n%5D - cost%5Bj%25n%5D
+total += gas%5Bi%5D - cost%5Bi%5D%0A            if total %3C subsum:
 %0A   
@@ -369,14 +369,22 @@
     
-j += 1
+subsum = total
 %0A   
@@ -400,85 +400,66 @@
     
-coun
+star
 t 
-+
 = 
-1%0A            if count == n:%0A                return i%0A        return -1
+i + 1%0A        return -1 if total %3C 0 else (start %25 n)
 %0A

make a proof, o(n) complexity