g | x | w | all
Bytes Lang Time Link
014APLDyalog Unicode250509T154747ZMat_rdv
054Janet250508T180519ZAdam
048Juby250508T143655ZJordan
011Pyth170726T221103ZAnders K
006Japt170723T203134ZJustin M
091Java170725T090249ZRCB
053Haskell170724T162506Znimi
013Brachylog170724T141221ZErik the
00605AB1E170724T140049ZErik the
058C++14170723T215313Zvaultah
056Python 3170723T215106Zovs
025Mathematica170724T084307Zuser6198
013CJam170724T081029ZPeter Ta
065Cubix170724T024159ZMickyT
032R170723T230700ZMickyT
051PHP170723T223234ZTitus
012Pyth170723T220150Znotjagan
060Mathematica170723T212502ZZaMoC
038JavaScript ES6170723T210113ZNeil
072Python170723T202041ZUriel
004Jelly170723T204759ZLeaky Nu
003Husk170723T204048ZZgarb
006MATL170723T202328ZLuis Men

APL(Dyalog Unicode), 14 bytes SBCS

+/2⊥¨⊢⊂⍨1,2≠/,

Try it on APLgolf!

1 and ¯1 are used as values.

Explanation

             ,   ravel (vectors stay the same, scalars turn into 1-element vector)
            /    reduce
           ≠     not equal
          2≠/,   2-wise reduce: where the sequence values ​​change
         ,       concatenate
        1,       concatenate 1 with...
       ⍨         reverse order of arguments
      ⊂          partition
     ⊢           the values
     ⊢⊂⍨1,2≠/,  splitting the sequence into parts with identical values
    ¨            each
   ⊥             decode
  2⊥             decode from binary (same as 1+2+4+8+... for positive parts and -1-2-4-8-... for negative parts)
 /               reduce
+/               sum
+/2⊥¨⊢⊂⍨1,2≠/, solution

Janet, 55 54 bytes

|(+;(do(var a 0)(map|(+= a(if(>(* a $)0)a(- $ a)))$)))                                                                                  

J-uby, 48 bytes

Same technique as Anders and Luis.

:slice_when+:!=|:sum+:*%[:first,:+@|:**&2|~:-&1]

Attempt This Online!

Explanation

:slice_when + :!= | :sum + :* % [:first, :+@ | :** & 2 | ~:- & 1]

:slice_when + :!= |                            # Slice array into runs of equal elements, then
:sum + :* % [      ,                        ]  # Sum products of
             :first                            #   The first element of each run and
                     :+@ | :** & 2 | ~:- & 1   #   2**n-1, where n is the size of the run

Pyth, 11 bytes

s.b*t^2NYr8

Try it online

How it works

         r8    run-length encode input
 .b            map (N, Y) ↦
     ^2N           2^N
    t              minus 1
   *    Y          times Y
s              sum

Japt, 8 6 bytes

-2 bytes thanks to @ETHproductions

ò¦ xì2

Try it online!

Explanation

Implicit input: [1, 1, 1, -1, -1, -1, -1, 1, 1]

ò¦

Partition input array (ò) between different (¦) elements:
[[1, 1, 1], [-1, -1, -1, -1], [1, 1]]

ì2

Map each partition to itself parsed as an array of base-2 digits (ì): [7, -15, 3]

x

Get the sum (x) of the resulting array: -5

Java, 91 bytes

int f(int[]a){int s=0,r=0,i=-1;while(++i<a.length)r+=s=s!=0&s>0==a[i]>0?2*s:a[i];return r;}

Haskell, 54 53 bytes

k#(a:b:c)=k+last(b:[k*2|a==b])#(b:c)
k#_=k
(#)=<<head

Try it online!

A simple recursion that either doubles the accumulator k or resets it to 1/-1 and adds the values of each step.

Brachylog, 13 bytes

ḅ⟨{ȧᵐ~ḃ}×h⟩ᵐ+

Try it online!

Brachylog uses _ instead of -.

Explanation:

?ḅ⟨{ȧᵐ~ḃ}×h⟩ᵐ+. Predicate (implicit ?.)
?               The input
 ḅ              A list where its elements' elements are equal, and when concatenated is ?
            ᵐ   The result of mapping this predicate over ?
  ⟨        ⟩      The result of forking two predicates over ? with a third
   {    }           The result of this predicate on ?
     ᵐ                The result of mapping this predicate over ?
    ȧ                   The absolute value of ?
      ~               An input where the result of this predicate over it is ?
       ḃ                A list that represents the digits of ? in base I (default 2)
          h         An object that is the first element of ?
         ×          A number that is the product of ?
             +  A number that is the sum of ?
              . The output

05AB1E, 6 bytes

γε2β}O

Try it online!

C++14, 58 bytes

[](auto v,int&s){int p=s=0;for(auto x:v)s+=p=x*p<1?x:2*p;}

Takes input via the v argument (std::vector, or any iterable container), outputs to the s argument (by reference). Each element of v must be either 1 or -1.

Example usage and test cases.

Python 3, 57 56 bytes

-1 byte thanks to @notjagan

f=lambda a,*s,m=1:m*a+(s>()and f(*s,m=(m*2)**(a==s[0])))

Try it online!

Mathematica, 25 bytes

Tr[Fold[#+##&]/@Split@#]&

CJam (13 bytes)

{e`{(*2b}%1b}

Online test suite. This is an anonymous block (function) which takes an array of ints on the stack and leaves an int on the stack. The last test shows that it handles an empty array correctly, giving 0.

The approach is straightforward run-length encoding followed by a manual run-length decode of each run and base conversion. Using the built-in for run-length decode I get one byte more with {e`1/:e~2fb1b} or {e`{ae~2b}%1b}.

Cubix, 65 bytes

W(?\q.p)w.;0.w;/0>I!U-unP(nwUs;q\^q:;^!u?P(w!u+w.;;>2p!u/@Os..sr\

Try it online!

        W ( ? \
        q . p )
        w . ; 0
        . w ; /
0 > I ! U - u n P ( n w U s ; q
\ ^ q : ; ^ ! u ? P ( w ! u + w
. ; ; > 2 p ! u / @ O s . . s r
\ . . . . . . . . . . . . . . .
        . . . .
        . . . .
        . . . .
        . . . .

Watch it run

As a brief explanation of this:

R, 32 bytes

sum((2^(R=rle(scan()))$l-1)*R$v)

Try it online!

This is the same method as a few others here.

With the input of -1 -1 1 1 -1 -1 -1

PHP, 51 bytes

while($d=$argv[++$i])$s+=$x=$d*$x>0?2*$x:$d;echo$s;

Run with -n or try it online.

Pyth, 12 bytes

sm*edt^2hdr8

Try it online!

Mathematica, 60 bytes

Tr[Last@*Accumulate/@(#[[1]]2^(Range@Tr[1^#]-1)&/@Split@#)]&

JavaScript (ES6), 38 bytes

a=>a.map(e=>r+=d=d*e>0?d+d:e,r=d=0)&&r

Python, 76 72 bytes

f=lambda s,k=1:len(s)and(f(s,s[0])if s[0]*abs(k)/k-1else k+f(s[1:],2*k))

Try it online!

Jelly, 4 bytes

ŒgḄS

Try it online!

Husk, 3 bytes

ṁḋg

Try it online!

Explanation

ṁḋg
  g  Group equal adjacent elements,
ṁ    take sum of
 ḋ   base-2 decoding of each group.

MATL, 6 bytes

Y'Wq*s

Try it online! Or verify all test cases.

Explanation

Consider input [1 1 1 1 1 1 -1 -1 -1 -1 1 1 1].

     % Implicit input
     % STACK: [1 1 1 1 1 1 -1 -1 -1 -1 1 1 1]
Y'   % Run-length encoding
     % STACK: [1 -1 1], [6 4 3]
W    % Exponentiation with base 2, element-wise
     % STACK: [1 -1 1], [64 16 8]
q    % Subtract 1
     % STACK: [1 -1 1], [63 15 7]
*    % Multiply, element-wise
     % STACK: [63 -15 7]
s    % sum of array
     % STACK: 55
     % Implicit display