#include<bits/stdc++.h>
#define lld long long int
#define faster ios_base::sync_with_stdio(false);
#define pr pair< int,int >
#define ppr pair< int, pr >
#define vppr vector< ppr >
#define trace(x) cerr<<#x<<": "<<x<<'\n';
#define trace2(a,n) { cerr<<#a<<": ";for(int i=0;i<n;i++)cerr<<a[i]<<" ";cerr<<'\n'; }

using namespace std;

int main(){

	int n,k;
	cin>>n>>k;
	vppr process;
	vector< string > input(n);
	for(int i=0;i<n;i++){
		cin>>input[i];
		for(int j=0;j<input[i].size();j++){
			if(input[i][j]=='.'){
				int num = 0;
				if(j>0 && input[i][j-1]=='S')num++;
				if(j<input[i].size()-1 && input[i][j+1]=='S')num++;
				process.push_back(make_pair(num,make_pair(i,j)));
			}
		}
	}
	sort(process.begin(),process.end());
	for(int i=0;i<k;i++)
		input[process[i].second.first][process[i].second.second] = 'x';

	for(int i=0;i<n;i++)cout<<input[i]<<'\n';

}


#include <iostream>
#include <vector>
#include <string>

using namespace std;

vector <vector <char> > a;
int n, k;

void seating(int x) {
	for (int i = 0; i < n; i++) {
		for (int j = 1; j <= 12 && k > 0; j++) {
			if (a[i][j] != '.') {
				continue;
			}
			int t = 0;
			if (a[i][j - 1] == 'S') {
				t++;
			}
			if (a[i][j + 1] == 'S') {
				t++;
			}
			if (t == x) {
				k--;
				a[i][j] = 'x';
			}
		}
	}
}

int main() {
	cin >> n >> k;
	a.resize(n, vector <char> (14, '-'));
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		for (int j = 1; j <= 12; j++) {
			a[i][j] = s[j - 1];
		}
	}

	seating(0);
	seating(1);
	seating(2);

	int ans = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 1; j <= 12; j++) {
			if (a[i][j] != 'S') {
				continue;
			}
			char nei = a[i][j - 1];
			if (nei == 'S' || nei == 'x' || nei == 'P') {
				ans ++;
			}
			nei = a[i][j + 1];
			if (nei == 'S' || nei == 'x' || nei == 'P') {
				ans ++;
			}
		}
	}
	cout << ans << "\n";
	for (int i = 0; i < n; i++) {
		for (int j = 1; j <= 12; j++) {
			cout << a[i][j];
		}
		cout << "\n";
	}
}


#include <iostream>

using namespace std;

int main(){
    int n, k;
    cin>>n>>k;
    char rows[n][12];
    for(int i=0;i<n;++i)
        for(int j=0;j<12;++j)
            cin>>rows[i][j];
    int prev = 0, zero = 0, one = 0, two = 0;
    for(int i=0;i<n;++i){
        for(int j=0;j<12;++j){
            int neighbours = 0;
            if(j>0 && rows[i][j-1]=='S'){
                ++neighbours;
            }
            if(j<11 && rows[i][j+1]=='S'){
                ++neighbours;
            }
            if(rows[i][j]=='P'||rows[i][j]=='S'){
                prev += neighbours;
            }else if(rows[i][j]=='.'){
                if(neighbours==0){
                    ++zero;
                }else if(neighbours==1){
                    ++one;
                }else{
                    ++two;
                }
            }
        }
    }
    one = min(one, max(0, k-zero));
    two = max(0, k-one-zero);
    cout<<prev+one+2*two<<endl;
    for(int i=0;i<n;++i){
        for(int j=0;j<12;++j){
            if(rows[i][j]=='.'){
                int neighbours = 0;
                if(j>0 && rows[i][j-1]=='S'){
                    ++neighbours;
                }
                if(j<11 && rows[i][j+1]=='S'){
                    ++neighbours;
                }
                if(neighbours==0){
                    cout<<'x';
                }else if(neighbours==1 && one>0){
                    cout<<'x';
                    --one;
                }else if(neighbours==2 && two>0){
                    cout<<'x';
                    --two;
                }else{
                    cout<<'.';
                }
            }else{
                cout<<rows[i][j];
            }
        }
        cout<<endl;
    }
}


#include <iostream>

using namespace std;

int main(){
    int n, k;
    cin>>n>>k;
    char rows[n][12];
    for(int i=0;i<n;++i)
        for(int j=0;j<12;++j)
            cin>>rows[i][j];
    int prev = 0, zero = 0, one = 0, two = 0;
    for(int i=0;i<n;++i){
        for(int j=0;j<12;++j){
            int neighbours = 0;
            if(j>0 && rows[i][j-1]=='S'){
                ++neighbours;
            }
            if(j<11 && rows[i][j+1]=='S'){
                ++neighbours;
            }
            if(rows[i][j]=='P'||rows[i][j]=='S'){
                prev += neighbours;
            }else if(rows[i][j]=='.'){
                if(neighbours==0){
                    ++zero;
                }else if(neighbours==1){
                    ++one;
                }else{
                    ++two;
                }
            }
        }
    }
    zero = min(k, zero);
    one = min(one, max(0, k-zero));
    two = max(0, k-one-zero);
    cout<<prev+one+2*two<<endl;
    for(int i=0;i<n;++i){
        for(int j=0;j<12;++j){
            if(rows[i][j]=='.'){
                int neighbours = 0;
                if(j>0 && rows[i][j-1]=='S'){
                    ++neighbours;
                }
                if(j<11 && rows[i][j+1]=='S'){
                    ++neighbours;
                }
                if(neighbours==0 && zero>0){
                    cout<<'x';
                    --zero;
                }else if(neighbours==1 && one>0){
                    cout<<'x';
                    --one;
                }else if(neighbours==2 && two>0){
                    cout<<'x';
                    --two;
                }else{
                    cout<<'.';
                }
            }else{
                cout<<rows[i][j];
            }
        }
        cout<<endl;
    }
}


#include <cstdio>
#include <string>
#include <vector>

using namespace std;

const int n_seats = 3 + 1 + 4 + 1 + 3;

void solve(vector<string> &plane, int k) {
    if (k == 0)
        return;
    // free spaces
    size_t n = plane.size();
    for (int allowed_occupied_neighbours = 0; ; ++allowed_occupied_neighbours) {
        for (size_t i = 0; i < n; ++i) {
            for (int j = 1; j <= n_seats; ++j) {
                if (plane[i][j] != '.')
                    continue;
                int occupied_neighbours = int(plane[i][j-1] == 'S') + int(plane[i][j + 1] == 'S');

                if (occupied_neighbours <= allowed_occupied_neighbours) {
                    plane[i][j] = 'x';
                    if (--k == 0) {
                        return;
                    }
                }
            }
        }
    }
}

int main() {
    int n, k;
    scanf("%d%d\n", &n, &k);
    vector<string> plane((size_t)n, string((size_t)n_seats + 2, '-'));
    for (int i = 0; i < n; ++i) {
        scanf(" ");
        for (int j = 1; j <= n_seats; ++j) {
            plane[i][j] = getchar();
        }
    }
    solve(plane, k);
    int result = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 1; j <= n_seats; ++j) {
            if (plane[i][j] != 'S')
                continue;
            result += !strchr(".-", plane[i][j - 1]) + !strchr(".-", plane[i][j + 1]);
        }
    }
    printf("%d\n", result);
    for (int i = 0; i < n; ++i) {
        printf("%s\n", plane[i].substr(1, n_seats).c_str());
    }
}


#include <cstdio>
#include <string>
#include <vector>
#include <cstring>

using namespace std;

const int n_seats = 3 + 1 + 4 + 1 + 3;

void solve(vector<string> &plane, int k) {
    if (k == 0)
        return;
    // free spaces
    size_t n = plane.size();
    for (int allowed_occupied_neighbours = 0; ; ++allowed_occupied_neighbours) {
        for (size_t i = 0; i < n; ++i) {
            for (int j = 1; j <= n_seats; ++j) {
                if (plane[i][j] != '.')
                    continue;
                int occupied_neighbours = int(plane[i][j-1] == 'S') + int(plane[i][j + 1] == 'S');

                if (occupied_neighbours <= allowed_occupied_neighbours) {
                    plane[i][j] = 'x';
                    if (--k == 0) {
                        return;
                    }
                }
            }
        }
    }
}

int main() {
    int n, k;
    scanf("%d%d\n", &n, &k);
    vector<string> plane((size_t)n, string((size_t)n_seats + 2, '-'));
    for (int i = 0; i < n; ++i) {
        scanf(" ");
        for (int j = 1; j <= n_seats; ++j) {
            plane[i][j] = getchar();
        }
    }
    solve(plane, k);
    int result = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 1; j <= n_seats; ++j) {
            if (plane[i][j] != 'S')
                continue;
            result += !strchr(".-", plane[i][j - 1]) + !strchr(".-", plane[i][j + 1]);
        }
    }
    printf("%d\n", result);
    for (int i = 0; i < n; ++i) {
        printf("%s\n", plane[i].substr(1, n_seats).c_str());
    }
}


#include<bits/stdc++.h>
using namespace std;
int a,s,d,f,g,h,j,k,l,i,n,m;
priority_queue<pair<int,pair<int,int>>>p;
pair<int,pair<int,int>>q;
main(){
string x[102];
cin>>n>>m;
for(i=0;i<n;i++){
cin>>x[i];
for(a=0;a<x[i].size();a++){

q={0,{i,a}};
if(a&&x[i][a-1]=='S'){q.first--;l++;}
if(a<x[i].size()-1&&x[i][a+1]=='S'){q.first--;l++;}if(x[i][a]=='-')l+=q.first;
if(x[i][a]!='.')continue;l+=q.first;
p.push(q);
}
}
for(i=0;i<m;i++){
x[p.top().second.first][p.top().second.second]='x';
l-=p.top().first;
p.pop();
}
cout<<l<<endl;
for(i=0;i<n;i++)cout<<x[i]<<endl;
}


#include <iostream>

using namespace std;
char data[101][1401];

int main()
{
    int n, k;
    cin >> n >> k;

    int numOfNeighbours = 0;

    for (int i = 0; i < n; i++) {
        data[i][0] = '.';
        data[i][13] = '.';
        for (int j = 1; j < 13; j++) {
            cin >> data[i][j];
        }
        for (int j = 1; j < 13; j++) {
            if (k == 0) {
                break;
            }

            if (data[i][j] == 'S') {
                if (data[i][j - 1] == 'S' || data[i][j - 1] == 'P') {
                    numOfNeighbours++;
                }
                if (data[i][j + 1] == 'S' || data[i][j + 1] == 'P') {
                    numOfNeighbours++;
                }
            }
            if (data[i][j] != '.') {
                continue;
            }
            int numOfSPasses = 0;
            if (data[i][j - 1] == 'S') {
                numOfSPasses++;
            }
            if (data[i][j + 1] == 'S') {
                numOfSPasses++;
            }

            if (numOfSPasses == 0) {
                k--;
                data[i][j] = 'x';
            }
            //cout << "k = " << k << "; j = " << j << "; ";
        }

    }
    //cout << "end of phase 0" << endl;
    if (k > 0) {
        for (int i = 0; i < n; i++) {
            for (int j = 1; j < 13; j++) {
                if (k == 0) {
                    break;
                }

                if (data[i][j] != '.') {
                    continue;
                }
                int numOfSPasses = 0;
                if (data[i][j - 1] == 'S') {
                    numOfSPasses++;
                }
                if (data[i][j + 1] == 'S') {
                    numOfSPasses++;
                }

                if (numOfSPasses == 1) {
                    k--;
                    data[i][j] = 'x';
                    numOfNeighbours++;
                }
                //cout << "k = " << k << "; j = " << j << "; ";
            }
        }
        //cout << "end of phase 1" << endl;
        if (k > 0) {
            for (int i = 0; i < n; i++) {
                for (int j = 1; j < 13; j++) {
                    if (k == 0) {
                        break;
                    }

                    if (data[i][j] != '.') {
                        continue;
                    }
                    int numOfSPasses = 0;
                    if (data[i][j - 1] == 'S') {
                        numOfSPasses++;
                    }
                    if (data[i][j + 1] == 'S') {
                        numOfSPasses++;
                    }

                    if (numOfSPasses == 2) {
                        k--;
                        data[i][j] = 'x';
                        numOfNeighbours+= 2;
                    }
                    //cout << "k = " << k << "; j = " << j << "; ";
                }
            }
        }
    }

    cout << numOfNeighbours << endl;
    for (int i = 0; i < n; i++) {
        for (int j = 1; j < 13; j++) {
            cout << data[i][j];
        }
        cout << endl;
    }

    return 0;
}


#include <iostream>

using namespace std;
char data[101][1401];

int main()
{
    int n, k;
    cin >> n >> k;

    int numOfNeighbours = 0;

    for (int i = 0; i < n; i++) {
        data[i][0] = '.';
        data[i][13] = '.';
        for (int j = 1; j < 13; j++) {
            cin >> data[i][j];
        }
        for (int j = 1; j < 13; j++) {
            if (k == 0) {
                break;
            }

            if (data[i][j] != '.') {
                continue;
            }
            int numOfSPasses = 0;
            if (data[i][j - 1] == 'S') {
                numOfSPasses++;
            }
            if (data[i][j + 1] == 'S') {
                numOfSPasses++;
            }

            if (numOfSPasses == 0) {
                k--;
                data[i][j] = 'x';
            }
            //cout << "k = " << k << "; j = " << j << "; ";
        }

    }
    //cout << "end of phase 0" << endl;
    if (k > 0) {
        for (int i = 0; i < n; i++) {
            for (int j = 1; j < 13; j++) {
                if (k == 0) {
                    break;
                }

                if (data[i][j] != '.') {
                    continue;
                }
                int numOfSPasses = 0;
                if (data[i][j - 1] == 'S') {
                    numOfSPasses++;
                }
                if (data[i][j + 1] == 'S') {
                    numOfSPasses++;
                }

                if (numOfSPasses == 1) {
                    k--;
                    data[i][j] = 'x';
                    numOfNeighbours++;
                }
                //cout << "k = " << k << "; j = " << j << "; ";
            }
        }
        //cout << "end of phase 1" << endl;
        if (k > 0) {
            for (int i = 0; i < n; i++) {
                for (int j = 1; j < 13; j++) {
                    if (k == 0) {
                        break;
                    }

                    if (data[i][j] != '.') {
                        continue;
                    }
                    int numOfSPasses = 0;
                    if (data[i][j - 1] == 'S') {
                        numOfSPasses++;
                    }
                    if (data[i][j + 1] == 'S') {
                        numOfSPasses++;
                    }

                    if (numOfSPasses == 2) {
                        k--;
                        data[i][j] = 'x';
                        numOfNeighbours+= 2;
                    }
                    //cout << "k = " << k << "; j = " << j << "; ";
                }
            }
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 1; j < 13; j++) {
            if (data[i][j] == 'S') {
                if (data[i][j - 1] == 'S' || data[i][j - 1] == 'P') {
                    numOfNeighbours++;
                }
                if (data[i][j + 1] == 'S' || data[i][j + 1] == 'P') {
                    numOfNeighbours++;
                }
            }
        }
    }

    cout << numOfNeighbours << endl;
    for (int i = 0; i < n; i++) {
        for (int j = 1; j < 13; j++) {
            cout << data[i][j];
        }
        cout << endl;
    }

    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<char>> a(n, vector<char>(12));
    for (int i = 0; i < n; i++)
        for (int j = 0; j < 12; j++)
            cin >> a[i][j];
    int ans = 0;
    vector<pair<int, pair<int, int>>> p;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < 12; j++)
            if (a[i][j] == '.')
            {
                int cnt = 0;
                if (j != 0 && a[i][j - 1] == 'S')
                    cnt++;
                if (j != 11 && a[i][j + 1] == 'S')
                    cnt++;
                p.push_back({cnt, {i, j}});
            }
            else if (a[i][j] == 'P' || a[i][j] == 'S')
            {
                if (j != 0 && a[i][j - 1] == 'S')
                    ans++;
                if (j != 11 && a[i][j + 1] == 'S')
                    ans++;
            }
    sort(p.begin(), p.end());
    for (int i = 0; i < m; i++)
    {
        ans += p[i].first;
        a[p[i].second.first][p[i].second.second] = 'x';
    }
    cout << ans << endl;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 12; j++)
            cout << a[i][j];
        cout << endl;
    }
}


#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::min;

enum
{
    STATUS = 's',
    USUAL = 'P',
    FREE = '.',
    STRING_LEN = 10
};

int
main()
{
    int n, k;
    cin >> n >> k;

    vector<string> plan;
    plan.reserve(n);

    int sadness = 0;
    int nil_places = 0;
    int uno_places = 0;
    int duo_places = 0;

    string buffer;
    buffer.reserve(STRING_LEN);
    for (int i = 0; i < n; i++) {
        cin >> buffer;

        int previous = '-';
        for (auto current = buffer.begin(), end = buffer.end(); current != end; current++) {
            if (*current == '.') {
                int neighbors = 0;
                if (previous == 'S') {
                    neighbors++;
                }
                if (*(current + 1) == 'S') {
                    neighbors++;
                }

                switch (neighbors) {
                case 0:
                    nil_places++;
                    *current = '0';
                    break;
                case 1:
                    uno_places++;
                    *current = '1';
                    break;
                default:
                    duo_places++;
                    *current = '2';
                    break;
                }
            } else if (*current == 'S') {
                if (previous == 'S') {
                    sadness += 2;
                } else if (previous == 'P') {
                    sadness++;
                }
            } else if (*current == 'P') {
                if (previous == 'S') {
                    sadness++;
                }
            }

            previous = *current;
        }

        plan.push_back(buffer);
    }

    int fill_nil = min(nil_places, k);
    k -= fill_nil;
    int fill_uno = min(uno_places, k);
    k -= fill_uno;
    int fill_duo = k;

    sadness += 2 * fill_duo + fill_uno;
    cout << sadness << endl;

    for (auto iter: plan) {
        buffer = iter;
        for (auto place = buffer.begin(), end = buffer.end(); place != end; place++) {
            switch (*place) {
            case '0':
                if (fill_nil > 0) {
                    *place = 'x';
                    fill_nil--;
                    break;
                }
            case '1':
                if (fill_uno > 0) {
                    *place = 'x';
                    fill_uno--;
                    break;
                }
            case '2':
                if (fill_duo > 0) {
                    *place = 'x';
                    fill_duo--;
                } else {
                    *place = '.';
                }
                break;
            default:
                break;
            }
        }
        cout << buffer << endl;
    }

    return 0;
}


//
// Created by Anya on 02.03.2018.
//
#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n, k;
    cin >> n >> k;
    vector<vector<char>> plan(n);
    for (int i = 0; i < n; ++i) {
        plan[i].push_back('-');
        for (int j = 0; j < 12; ++j) {
            char x;
            cin >> x;
            plan[i].push_back(x);
        }
        plan[i].push_back('-');
    }
    int count1 = 0, count2 = 0, count0 = 0;
    int ans = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 1; j < 13; ++j) {
            if (plan[i][j] == '.') {
                if (plan[i][j-1] == 'S' && plan[i][j+1] == 'S') {
                    ++count2;
                } else if (plan[i][j-1] == 'S' || plan[i][j+1] == 'S') {
                    ++count1;
                } else {
                    ++count0;
                }
            }
            if (plan[i][j] == 'S') {
                if (plan[i][j - 1] == 'S' || plan[i][j - 1] == 'P') {
                    ++ans;
                    cout << j << endl;
                }
                if (plan[i][j + 1] == 'S' || plan[i][j + 1] == 'P') {
                    ++ans;
                    cout << j << endl;
                }
            }
        }
    }
    count0 = min(count0, k);
    k -= count0;
    count1 = min(count1, k);
    k -= count1;
    count2 = k;
    ans += count1 + count2 * 2;
    cout << count0 << ' ' << count1 << ' ' << count2 << endl;
    cout << ans << endl;
    for (int i = 0; i < n; ++i) {
        for (int j = 1; j < 13; ++j) {
            if (plan[i][j] == '.') {
                bool isSS = plan[i][j-1] == 'S' && plan[i][j+1] == 'S', isS = plan[i][j-1] == 'S' || plan[i][j+1] == 'S';
                if (isSS && count2 > 0) {
                    --count2;
                    //cout << count2 << endl;
                    plan[i][j] = 'x';
                } else if (!isSS && isS && count1 > 0) {
                    --count1;
                    plan[i][j] = 'x';
                } else if (!isSS && !isS && count0 > 0) {
                    --count0;
                    plan[i][j] = 'x';
                }
            }
            cout << plan[i][j];
        }
        cout << endl;
    }
    /*for (int i = 0; i < n; ++i) {
        for (char x : plan[i]) {
            cout << x;
        }
        cout << endl;
    }*/
    return 0;
}


//
// Created by Anya on 02.03.2018.
//
#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n, k;
    cin >> n >> k;
    vector<vector<char>> plan(n);
    for (int i = 0; i < n; ++i) {
        plan[i].push_back('-');
        for (int j = 0; j < 12; ++j) {
            char x;
            cin >> x;
            plan[i].push_back(x);
        }
        plan[i].push_back('-');
    }
    int count1 = 0, count2 = 0, count0 = 0;
    int ans = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 1; j < 13; ++j) {
            if (plan[i][j] == '.') {
                if (plan[i][j-1] == 'S' && plan[i][j+1] == 'S') {
                    ++count2;
                } else if (plan[i][j-1] == 'S' || plan[i][j+1] == 'S') {
                    ++count1;
                } else {
                    ++count0;
                }
            }
            if (plan[i][j] == 'S') {
                if (plan[i][j - 1] == 'S' || plan[i][j - 1] == 'P') {
                    ++ans;
                    //cout << j << endl;
                }
                if (plan[i][j + 1] == 'S' || plan[i][j + 1] == 'P') {
                    ++ans;
                    //cout << j << endl;
                }
            }
        }
    }
    count0 = min(count0, k);
    k -= count0;
    count1 = min(count1, k);
    k -= count1;
    count2 = k;
    ans += count1 + count2 * 2;
    //cout << count0 << ' ' << count1 << ' ' << count2 << endl;
    cout << ans << endl;
    for (int i = 0; i < n; ++i) {
        for (int j = 1; j < 13; ++j) {
            if (plan[i][j] == '.') {
                bool isSS = plan[i][j-1] == 'S' && plan[i][j+1] == 'S', isS = plan[i][j-1] == 'S' || plan[i][j+1] == 'S';
                if (isSS && count2 > 0) {
                    --count2;
                    //cout << count2 << endl;
                    plan[i][j] = 'x';
                } else if (!isSS && isS && count1 > 0) {
                    --count1;
                    plan[i][j] = 'x';
                } else if (!isSS && !isS && count0 > 0) {
                    --count0;
                    plan[i][j] = 'x';
                }
            }
            cout << plan[i][j];
        }
        cout << endl;
    }
    /*for (int i = 0; i < n; ++i) {
        for (char x : plan[i]) {
            cout << x;
        }
        cout << endl;
    }*/
    return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>

#define PI 3.1415926535897932
#define MOD 1000000007
#define EPS 0.0000001
#define INF 2147483647
#define LINF 9223372036854775807
#define FAST ios::sync_with_stdio(0); cin.tie(0)
#define endl "\n"
#define COUT_DOUBLE cout << fixed << setprecision(12)
#define FILEIN freopen("input.txt", "r", stdin)
#define FILEOUT freopen("output.txt", "w", stdout)
#define FILEDEF FILEIN; FILEOUT

using namespace std;

int fun(vector<vector<int>> a, int i, int j) {
    int ans = 0;
    if (j > 0 && a[i][j - 1] == 1) ans++;
    if (j < a[i].size() - 1 && a[i][j + 1] == 1) ans++;

    return ans;
}

void fun2(int &a, int &x) {
    if (a >= x) {
        int tmp = a;
        a -= x;
        x -= tmp - a;
    }
    else {
        x -= a;
        a = 0;
    }
}

int main() {
    FAST;
    int n, k, minans = 0;
    cin >> n >> k;
    vector<vector<int>> a;

    for (int i = 0; i < n; i++) {
        a.push_back({});

        string s;
        cin >> s;
        for (auto c : s) {
            if (c == '-') a.push_back({});
            else if (c == 'S') a[a.size() - 1].push_back(1);
            else if (c == '.') a[a.size() - 1].push_back(0);
            else if (c == 'P') a[a.size() - 1].push_back(-1);
        }
    }

    int zero = 0, one = 0, two = 0;
    for (int i = 0; i < a.size(); i++) {
        for (int j = 0; j < a[i].size(); j++) {
            if (a[i][j] == 1) {
                if (j > 0 && a[i][j - 1]) minans++;
                if (j < a[i].size() - 1 && a[i][j + 1]) minans++;
            }
            if (a[i][j] == 0) {
                int b = fun(a, i, j);

                if (b == 0) zero++;
                else if (b == 1) one++;
                else if (b == 2) two++;
            }

        }
    }

    int x = zero + one + two - k;
    fun2(two, x);
    fun2(one, x);
    fun2(zero, x);

    for (int i = 0; i < a.size(); i++) {
        for (int j = 0; j < a[i].size(); j++) {
            if (a[i][j] != 0) continue;
            else if (fun(a, i, j) == 0 && zero) {
                a[i][j] = 2;
                zero--;
            }
            else if (fun(a, i, j) == 1 && one) {
                a[i][j] = 2;
                one--;
                minans++;
            }
            else if (fun(a, i, j) == 2 && two) {
                a[i][j] = 2;
                two--;
                minans += 2;
            }
        }
    }

    cout << minans << endl;

    for (int i = 0; i < a.size();) {
        for (int x = 0; x < 3; i++, x++) {
            for (int j = 0; j < a[i].size(); j++) {
                if (a[i][j] == -1) cout << "P";
                else if (a[i][j] == 0) cout << ".";
                else if (a[i][j] == 1) cout << "S";
                else cout << "x";
            }
            if (x != 2) cout << "-";
        }
        cout << endl;
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool contains(vector<int> &v, int x) {
	for (int i = 0; i < v.size(); i++) {
		if (v[i] == x) return true;
	}
	return false;
}

bool check(vector<vector<int>> &v, int x) {
	vector<int> xx;
	while (x > 0) {
		xx.push_back(x%10);
		x /= 10;
	}
	reverse(xx.begin(), xx.end());
	if (xx.size() > v.size()) return false;
	vector<vector<int>> can;
	for (int i = 0; i < xx.size(); i++) {
		vector<int> temp;
		for (int j = 0; j < v.size(); j++) {
			if (contains(v[j], xx[i])) {
				temp.push_back(j);
			}
		}
	}
	if (xx.size() == 0) {
		for (int i = 0; i < v.size(); i++) {
			if (contains(v[i], 0)) return true;
		}
		return false;
	} else if (xx.size() == 1) {
		for (int i = 0; i < v.size(); i++) {
			if (contains(v[i], xx[0])) return true;
		}
		return false;
	} else if (xx.size() == 2) {
		for (int i = 0; i < v.size(); i++) {
			for (int j = 0; j < v.size(); j++) {
				if (i != j && contains(v[i], xx[0]) && contains(v[j], xx[1]))
					return true;
			}
		}
		return false;
	} else if (xx.size() == 3) {
		for (int i = 0; i < v.size(); i++) {
			for (int j = 0; j < v.size(); j++) {
				for (int k = 0; k < v.size(); k++) {
					if (i != j && j != k && i != k) {
						if (contains(v[i], xx[0]) && contains(v[j], xx[1]) && contains(v[k], xx[2]))
							return true;
					}
				}
			}
		}
		return false;
	}
}

int main() {
	// freopen("a.in", "r", stdin);
	int n;
	cin >> n;
	vector<vector<int>> v;
	for (int i = 0; i < n; i++) {
		vector<int> tmp;
		for (int j = 0; j < 6; j++) {
			int x;
			cin >> x;
			tmp.push_back(x);
		}
		v.push_back(tmp);
	}
	for (int i = 1; i < 1000; i++) {
		if (!check(v, i)) {
			cout << i-1 << endl;
			return 0;
		}
	}
	cout << 999 << endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int x[] = {0, 0};
int y[] = {1, -1};
int main() {
	// freopen("a.in", "r", stdin);
	int n, k;
	cin >> n >> k;
	string s[110];
	vector<pair<int, pair<int, int>>> pts;
	for (int i = 0; i < n; i++) {
		cin >> s[i];
	}
	int m = s[0].size();
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (s[i][j] == '.') {
				int cnt = 0;
				for (int k = 0; k < 2; k++) {
					int ito = i + x[k];
					int jto = j + y[k];
					if (ito >= 0 && ito < n && jto >= 0 && jto < m) {
						if (s[ito][jto] == 'S') {
							cnt++;
						}
					}
				}
				pts.push_back(make_pair(cnt, make_pair(i, j)));
			}
		}
	}

	sort(pts.begin(), pts.end());

	for (int i = 0; i < k; i++) {
		pair<int, int> pt = pts[i].second;
		s[pt.first][pt.second] = 'x';
	}

	int ans = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (s[i][j] == 'S') {
				for (int k = 0; k < 2; k++) {
					int ito = i + x[k];
					int jto = j + y[k];
					if (ito >= 0 && ito < n && jto >= 0 && jto < m) {
						if (s[ito][jto] == 'S' || s[ito][jto] == 'P' || s[ito][jto] == 'x') {
							ans++;
						}
					}
				}
			}
		}
	}

	cout << ans << endl;
	for (int i = 0; i < n; i++) {
		cout << s[i] << endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
struct pass {
public:
  pass() : k(0), x(0), y(0) {};
  pass(int k, int x, int y) : k(k), x(x), y(y) {};
  int k;
  int x;
  int y;
};

bool cmp(pass a, pass b) {
  if (a.k < b.k)
    return true;
  else
    return false;
}

int main(int argc, const char * argv[]) {
  int n, k, sum = 0;
  int count_free = 0;
  std::cin >> n >> k;
  getchar();
  std::string line;
  std::vector<pass> p;
  std::vector<std::string> out;
  for (int i = 0; i < n; i++) {
    std::getline(std::cin, line);
    out.push_back(line);
    for (int j = 0; j < 12; j++) {

      int count = 0;
      int count_p = 0;
      if (j == 0) {
        if (line[1] == 'S')
          count++;
        if (line[1] == 'P')
          count_p++;
        //p.push_back(pass(count, i, j));
        }
      else
        if (j == 11) {
          if (line[10] == 'S')
            count++;
          if (line[10] == 'P')
            count_p++;
          //p.push_back(pass(count, i, j));
        }
      else
        if (j != 3 && j != 8) {
          if (line[j - 1] == 'S')
            count++;
          if (line[j + 1] == 'S')
            count++;
          if (line[j - 1] == 'P')
            count_p++;
          if (line[j + 1] == 'P')
            count_p++;
          //p.push_back(pass(count, i, j));
        }
      if (line[j] == '.') {
        count_free++;
        p.push_back(pass(count, i, j));
      }
      if (line[j] == 'S') {
        sum += count + count_p;
      }
    }
  }
  std::sort(p.begin(), p.end(), cmp);
  for (int i = 0; i < k; i++) {
    out[p[i].x][p[i].y]='x';
    sum += p[i].k;
  }
  std::cout << sum << std::endl;
  for (int i = 0; i < n; i++)
    std::cout << out[i] << std::endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
struct pass {
public:
  pass() : k(0), x(0), y(0) {};
  pass(int k, int x, int y) : k(k), x(x), y(y) {};
  int k;
  int x;
  int y;
};

bool cmp(pass a, pass b) {
  if (a.k < b.k)
    return true;
  else
    return false;
}

int main(int argc, const char * argv[]) {
  int n, k;
  int count_free = 0;
  std::cin >> n >> k;
  getchar();
  std::string line;
  std::vector<pass> p;
  std::vector<std::string> out;
  for (int i = 0; i < n; i++) {
    std::getline(std::cin, line);
    out.push_back(line);
    for (int j = 0; j < 12; j++) {
      if (line[j] == '.') {
        count_free++;
      int count = 0;
      if (j == 0) {
        if (line[1] == 'S')
          count++;
        p.push_back(pass(count, i, j));
        }
      else
        if (j == 11) {
          if (line[10] == 'S')
            count++;
          p.push_back(pass(count, i, j));
        }
      else
        if (j != 3 && j != 8) {
          if (line[j - 1] == 'S')
            count++;
          if (line[j + 1] == 'S')
            count++;
          p.push_back(pass(count, i, j));
        }
    }
    }
  }
  std::sort(p.begin(), p.end(), cmp);
  for (int i = 0; i < k; i++)
    out[p[i].x][p[i].y]='x';
  for (int i = 0; i < n; i++)
    std::cout << out[i] << std::endl;
  return 0;
}


#include <iostream>
#include <vector>
struct pass {
public:
  pass() : k(0), x(0), y(0) {};
  pass(int k, int x, int y) : k(k), x(x), y(y) {};
  int k;
  int x;
  int y;
};

bool cmp(pass a, pass b) {
  if (a.k < b.k)
    return true;
  else
    return false;
}

int main(int argc, const char * argv[]) {
  int n, k;
  int count_free = 0;
  std::cin >> n >> k;
  getchar();
  std::string line;
  std::vector<pass> p;
  std::vector<std::string> out;
  for (int i = 0; i < n; i++) {
    std::getline(std::cin, line);
    out.push_back(line);
    for (int j = 0; j < 12; j++) {
      if (line[j] == '.') {
        count_free++;
      int count = 0;
      if (j == 0) {
        if (line[1] == 'S')
          count++;
        p.push_back(pass(count, i, j));
        }
      else
        if (j == 11) {
          if (line[10] == 'S')
            count++;
          p.push_back(pass(count, i, j));
        }
      else
        if (j != 3 && j != 8) {
          if (line[j - 1] == 'S')
            count++;
          if (line[j + 1] == 'S')
            count++;
          p.push_back(pass(count, i, j));
        }
    }
    }
  }
  std::sort(p.begin(), p.end(), cmp);
  for (int i = 0; i < k; i++)
    out[p[i].x][p[i].y]='x';
  for (int i = 0; i < n; i++)
    std::cout << out[i] << std::endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>

using namespace std;

int main() {
    int n, k, ans = 0;
    vector<string> v;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        s = "-" + s + "-";
        v.push_back(s);
    }
    for (int p = 0; p < k; p++) {
        int x = -1, y = -1, miny = 3;
        for (int i = 0; i < n; i++)
        for (int j = 0; j < v[i].size(); j++) {
            if (v[i][j] != '.')
                continue;
            int neigh = 0;
            if (v[i][j - 1] == 'S')
                neigh++;
            if (v[i][j + 1] == 'S')
                neigh++;
            if (x == -1 || neigh < miny) {
                x = i;
                y = j;
                miny = neigh;
            }
        }
        ans += miny;
        v[x][y] = 'x';
    }
    for (int i = 0; i < n; i++)
    for (int j = 0; j < v[i].size(); j++)
    if (v[i][j] == 'S') {
        if (v[i][j - 1] == 'S' || v[i][j - 1] == 'P')
            ans++;
        if (v[i][j + 1] == 'S' || v[i][j + 1] == 'P')
            ans++;
    }
    cout << ans << endl;
    for (int i = 0; i < n; i++) {
        cout << v[i].substr(1, v[i].size() - 2) << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

#define D 107
#define Inf 1000000007

#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
typedef long double ld;

string s[D];
vector <pii> mesta[5];
int n, k, m = 12;

ll check(ll x, ll y)
{
    if (y == 1)
        return s[x][y + 1] == 'S';
    if (y == m)
        return s[x][y - 1] == 'S';

    return (ll) (s[x][y - 1] == 'S') + (ll) (s[x][y + 1] == 'S');
}

int main()
{
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
    {
        cin >> s[i];
        s[i] = ' ' + s[i];
        for (int j = 1; j <= m; j++)
        {
            if (s[i][j] == '.')
                mesta[check(i, j)].push_back({i, j});
        }
    }
    for (ll z = 0; z <= 2; z++)
        for (ll i = 0; i < mesta[z].size(); i++)
            if (k > 0)
            {
                s[mesta[z][i].fi][mesta[z][i].se] = 'x';
                k--;
            }
    ll res = 0;
    for (ll i = 1; i <= n; i++)
        for (ll j = 1; j <= m; j++)
            if (s[i][j] == 'S')
            {
                if (j != 1)
                    res += (s[i][j - 1] != '.') && (s[i][j - 1] != '-');
                if (j != m)
                    res += (s[i][j + 1] != '.') && (s[i][j + 1] != '-');
            }
    cout << res << endl;
    for (ll i = 1; i <= n; i++)
    {
        for (ll j = 1; j <= m; j++)
            cout << s[i][j];
        cout << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

//.define
//#define FILE_IN_OUT
#define RET(_x)                       return std::cout << (_x), 0;
#define all(_obj)                     std::begin(_obj), std::end(_obj)
#define fori(_iterator, _begin, _end) for (int _iterator = (_begin); _iterator < (_end); ++_iterator)
#define forr(_iterator, _begin, _end) for (int _iterator = (_begin); _iterator >= (_end); --_iterator)
#define loop(_n)                      for (int i = 0; i < (_n); ++i)
#define ceilDiv(_dividend, _divider)  (((_dividend) - 1LL + (_divider)) / (_divider))
#define sz(_obj)                      static_cast<int>(std::size(_obj))
#define endl                          "\n"
template<typename Tp>
inline void outarr(Tp _array, const std::size_t _N, const char* _delim = " ") {
    Tp end = _array + _N;
    for (Tp current = _array; current != end; ++current) {
        std::cout << *current << _delim;
    }
    std::cout << '\n';
}

//.constant
using ll  = int64_t;
using pii = std::pair<int, int>;
constexpr int INF = INT_MAX;
constexpr int MOD = static_cast<const int>(1e9 + 7);

//.data
pii arr[1000];
ll cnt[3];

inline ll C(int k, ll x) {
    if (k > x) {
        return 0;
    }
    int d = 1;
    ll res = x;
    fori(i, 1, k) {
        res *= x - i;
        d *= i + 1;
    }
    return res / d;
}

inline ll func(int t) {
    switch (t) {
        case 0:
            return (cnt[0] - 1) * C(2, cnt[1]) * C(3, cnt[2]);
        case 1:
            return cnt[0] * (cnt[1] - 1) * C(3, cnt[2]);
        default:
            return cnt[0] * C(2, cnt[1]) * C(2, cnt[2] - 1);
    }
}

inline bool check() {
    return cnt[0] && cnt[1] > 1 && cnt[2] > 2;
}

//.code
int main() {
#ifdef FILE_IN_OUT
    std::ifstream cin  ("input.txt",  std::ios::in);
    std::ofstream cout ("output.txt", std::ios::out);
#else
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
#endif

    int ng, nd, nf;
    cin >> ng >> nd >> nf;
    int n = ng + nd + nf;
    loop(ng) {
        cin >> arr[i].first;
        arr[i].second = 0;
    }
    fori(i, ng, ng + nd) {
        cin >> arr[i].first;
        arr[i].second = 1;
    }
    fori(i, ng + nd, n) {
        cin >> arr[i].first;
        arr[i].second = 2;
    }
    sort(arr, arr + n);

    ll ans = 0;
    int r = arr[0].first << 1;
    int j = 0;
    loop(n) {
        if (arr[i].first <= r) {
            ++cnt[arr[i].second];
        } else {
            if (check()) {
                ans += func(arr[j].second);
            }
            --cnt[arr[j++].second];
            r = arr[j].first << 1;
            --i;
        }
    }
    while (check()) {
        ans += func(arr[j].second);
        --cnt[arr[j++].second];
    }

    cout << ans << endl;

#ifdef FILE_IN_OUT
    cin.close();
    cout.close();
#endif
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

//.define
//#define FILE_IN_OUT
#define RET(_x)                       return std::cout << (_x), 0;
#define all(_obj)                     std::begin(_obj), std::end(_obj)
#define fori(_iterator, _begin, _end) for (int _iterator = (_begin); _iterator < (_end); ++_iterator)
#define forr(_iterator, _begin, _end) for (int _iterator = (_begin); _iterator >= (_end); --_iterator)
#define loop(_n)                      for (int i = 0; i < (_n); ++i)
#define ceilDiv(_dividend, _divider)  (((_dividend) - 1LL + (_divider)) / (_divider))
#define sz(_obj)                      static_cast<int>(std::size(_obj))
#define endl                          "\n"
template<typename Tp>
inline void outarr(Tp _array, const std::size_t _N, const char* _delim = " ") {
    Tp end = _array + _N;
    for (Tp current = _array; current != end; ++current) {
        std::cout << *current << _delim;
    }
    std::cout << '\n';
}

//.constant
using ll  = int64_t;
using pii = std::pair<int, int>;
constexpr int INF = INT_MAX;
constexpr int MOD = static_cast<const int>(1e9 + 7);

//.data
pii arr[1000];
ll cnt[3];

inline ll C(int k, ll x) {
    if (k > x) {
        return 0;
    }
    int d = 1;
    ll res = x;
    fori(i, 1, k) {
        res *= x - i;
        d *= i + 1;
    }
    return res / d;
}

inline ll func(int t) {
    switch (t) {
        case 0:
            return C(2, cnt[1]) * C(3, cnt[2]);
        case 1:
            return cnt[0] * (cnt[1] - 1) * C(3, cnt[2]);
        default:
            return cnt[0] * C(2, cnt[1]) * C(2, cnt[2] - 1);
    }
}

inline bool check() {
    return cnt[0] && cnt[1] > 1 && cnt[2] > 2;
}

//.code
int main() {
#ifdef FILE_IN_OUT
    std::ifstream cin  ("input.txt",  std::ios::in);
    std::ofstream cout ("output.txt", std::ios::out);
#else
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
#endif

    int ng, nd, nf;
    cin >> ng >> nd >> nf;
    int n = ng + nd + nf;
    loop(ng) {
        cin >> arr[i].first;
        arr[i].second = 0;
    }
    fori(i, ng, ng + nd) {
        cin >> arr[i].first;
        arr[i].second = 1;
    }
    fori(i, ng + nd, n) {
        cin >> arr[i].first;
        arr[i].second = 2;
    }
    sort(arr, arr + n);

    ll ans = 0;
    int r = arr[0].first << 1;
    int j = 0;
    loop(n) {
        if (arr[i].first <= r) {
            ++cnt[arr[i].second];
        } else {
            if (check()) {
                ans += func(arr[j].second);
            }
            --cnt[arr[j++].second];
            r = arr[j].first << 1;
            --i;
        }
    }
    while (check()) {
        ans += func(arr[j].second);
        --cnt[arr[j++].second];
    }

    cout << ans << endl;

#ifdef FILE_IN_OUT
    cin.close();
    cout.close();
#endif
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

//.define
//#define FILE_IN_OUT
#define RET(_x)                       return std::cout << (_x), 0;
#define all(_obj)                     std::begin(_obj), std::end(_obj)
#define fori(_iterator, _begin, _end) for (int _iterator = (_begin); _iterator < (_end); ++_iterator)
#define forr(_iterator, _begin, _end) for (int _iterator = (_begin); _iterator >= (_end); --_iterator)
#define loop(_n)                      for (int i = 0; i < (_n); ++i)
#define ceilDiv(_dividend, _divider)  (((_dividend) - 1LL + (_divider)) / (_divider))
#define sz(_obj)                      static_cast<int>(std::size(_obj))
#define endl                          "\n"
template<typename Tp>
inline void outarr(Tp _array, const std::size_t _N, const char* _delim = " ") {
    Tp end = _array + _N;
    for (Tp current = _array; current != end; ++current) {
        std::cout << *current << _delim;
    }
    std::cout << '\n';
}

//.constant
using ll  = int64_t;
using pii = std::pair<int, int>;
constexpr int INF = INT_MAX;
constexpr int MOD = static_cast<const int>(1e9 + 7);

//.data
pii arr[1000];
ll cnt[3];

inline ll C(int k, ll x) {
    if (k > x) {
        return 0;
    }
    int d = 1;
    ll res = x;
    fori(i, 1, k) {
        res *= x - i;
        d *= i + 1;
    }
    return res / d;
}

inline ll func(int t) {
    switch (t) {
        case 0:
            return (cnt[0] - 1) * C(2, cnt[1]) * C(3, cnt[2]);
        case 1:
            return cnt[0] * (cnt[1] - 1) * C(3, cnt[2]);
        default:
            return cnt[0] * C(2, cnt[1]) * C(2, cnt[2] - 1);
    }
}

inline bool check() {
    return cnt[0] && cnt[1] && cnt[2];
}

//.code
int main() {
#ifdef FILE_IN_OUT
    std::ifstream cin  ("input.txt",  std::ios::in);
    std::ofstream cout ("output.txt", std::ios::out);
#else
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
#endif

    int ng, nd, nf;
    cin >> ng >> nd >> nf;
    int n = ng + nd + nf;
    loop(ng) {
        cin >> arr[i].first;
        arr[i].second = 0;
    }
    fori(i, ng, ng + nd) {
        cin >> arr[i].first;
        arr[i].second = 1;
    }
    fori(i, ng + nd, n) {
        cin >> arr[i].first;
        arr[i].second = 2;
    }
    sort(arr, arr + n);

    ll ans = 0;
    int r = arr[0].first << 1;
    int j = 0;
    loop(n) {
        if (arr[i].first <= r) {
            ++cnt[arr[i].second];
        } else {
            if (check()) {
                ans += func(arr[j].second);
            }
            --cnt[arr[j++].second];
            r = arr[j].first << 1;
            --i;
        }
    }
    while (check()) {
        ans += func(arr[j].second);
        --cnt[arr[j++].second];
    }

    cout << ans << endl;

#ifdef FILE_IN_OUT
    cin.close();
    cout.close();
#endif
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

//.define
//#define FILE_IN_OUT
#define RET(_x)                       return std::cout << (_x), 0;
#define all(_obj)                     std::begin(_obj), std::end(_obj)
#define fori(_iterator, _begin, _end) for (int _iterator = (_begin); _iterator < (_end); ++_iterator)
#define forr(_iterator, _begin, _end) for (int _iterator = (_begin); _iterator >= (_end); --_iterator)
#define loop(_n)                      for (int i = 0; i < (_n); ++i)
#define ceilDiv(_dividend, _divider)  (((_dividend) - 1LL + (_divider)) / (_divider))
#define sz(_obj)                      static_cast<int>(std::size(_obj))
#define endl                          "\n"
template<typename Tp>
inline void outarr(Tp _array, const std::size_t _N, const char* _delim = " ") {
    Tp end = _array + _N;
    for (Tp current = _array; current != end; ++current) {
        std::cout << *current << _delim;
    }
    std::cout << '\n';
}

//.constant
using ll  = int64_t;
using pii = std::pair<int, int>;
constexpr int INF = INT_MAX;
constexpr int MOD = static_cast<const int>(1e9 + 7);

//.data
int f[1000];
int d[1000];
int g[1000];

inline int C(int n) {
    if (n < 3) {
        return 0;
    }
    return n * (n - 1) * (n - 2) / 6;
}

//.code
int main() {
#ifdef FILE_IN_OUT
    std::ifstream cin  ("input.txt",  std::ios::in);
    std::ofstream cout ("output.txt", std::ios::out);
#else
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
#endif

    int ng, nd, nf;
    cin >> ng >> nd >> nf;
    loop(ng) {
        cin >> g[i];
    }
    sort(g, g + ng);
    loop(nd) {
        cin >> d[i];
    }
    sort(d, d + nd);
    loop(nf) {
        cin >> f[i];
    }
    sort(f, f + nf);

    ll ans = 0;
    loop(ng) {
        int l = distance(d, lower_bound(d, d + nd, ceilDiv(g[i], 2)));
        int r = distance(d, upper_bound(d, d + nd, g[i] << 1));
        fori(j1, l, r) {
            fori(j2, j1 + 1, r) {
                int y1 = min(min(d[j1], d[j2]), g[i]) << 1;
                int y2 = ceilDiv(max(max(d[j1], d[j2]), g[i]), 2);
                int p11 = distance(f, upper_bound(f, f + nf, y1));
                int p21 = distance(f, lower_bound(f, f + nf, y2));
                if (p11 - p21 < 3) {
                    continue;
                }
                if ((f[p21] << 1) < f[p11 - 1]) {
                    int p12 = distance(f, lower_bound(f, f + nf, ceilDiv(f[p11 - 1], 2)));
                    int p22 = distance(f, upper_bound(f, f + nf, f[p21] << 1));
                    ans += C(p22 - p21);
                    ans += C(p11 - p12);
                    ans -= C(p22 - p12);
                } else {
                    ans += C(p11 - p21);
                }
            }
        }
    }

    cout << ans << endl;

#ifdef FILE_IN_OUT
    cin.close();
    cout.close();
#endif
    return 0;
}


#include<iostream>
using namespace std;

int num[100001];

int main()
{
	int g, d, f;
	cin�g�d�f;
	for (int i = 0; i<100001; i++) num[i] = 0;
	for (int i = 0; i<g; i++) {
		int a;
		cin�a;
		num[a] = 1;
	}
	for (int i = 0; i<d; i++)
	{
		int a;
		cin�a;
		num[a] = 2;
	}
	for (int i = 0; i<f; i++) {
		int a;
		cin�a;
		num[a] = 3;
	}
	int i = 100000, j = 50000, koln = 0, kolz = 0, kolv = 0, rez;
	for (int k = j; k <= i; k++)
	{
		switch (num[k]) {
		case 1:kolv++; break;
		case 2:kolz++; break;
		case 3:koln++; break;
		}
		rez = kolv * kolz*(kolz - 1)*koln*(koln - 1)*(koln - 2) / 12;
	}
	i -= 2;
	j -= 1;
	while (i - j>4) {
		switch (num[i + 2])
		{
		case 1:kolv--; break;
		case 2:kolz--; break;
		case 3:koln--; break;
		}
		switch (num[i + 1]) {
		case 1:kolv--; break;
		case 2:kolz--; break;
		case 3:koln--; break;
		}
		rez -= kolv * kolz*(kolz - 1)*koln*(koln - 1)*(koln - 2) / 12;
		switch (num[j]) {
		case 1:kolv++; break;
		case 2:kolz++; break;
		case 3:koln++; break;
		}
		rez += kolv * kolz*(kolz - 1)*koln*(koln - 1)*(koln - 2) / 12;
		i -= 2;
		j -= 1;
	}
	cout�rez�endl;
	return 0;
}


#include<iostream>
using namespace std;

int num[100001];

int main()
{
	int g, d, f;
	cin>>g>>d>>f;
	for (int i = 0; i<100001; i++) num[i] = 0;
	for (int i = 0; i<g; i++) {
		int a;
		cin>>a;
		num[a] = 1;
	}
	for (int i = 0; i<d; i++)
	{
		int a;
		cin>>a;
		num[a] = 2;
	}
	for (int i = 0; i<f; i++) {
		int a;
		cin>>a;
		num[a] = 3;
	}
	int i = 100000, j = 50000, koln = 0, kolz = 0, kolv = 0, rez;
	for (int k = j; k <= i; k++)
	{
		switch (num[k]) {
		case 1:kolv++; break;
		case 2:kolz++; break;
		case 3:koln++; break;
		}
		rez = kolv * kolz*(kolz - 1)*koln*(koln - 1)*(koln - 2) / 12;
	}
	i -= 2;
	j -= 1;
	while (i - j>4) {
		switch (num[i + 2])
		{
		case 1:kolv--; break;
		case 2:kolz--; break;
		case 3:koln--; break;
		}
		switch (num[i + 1]) {
		case 1:kolv--; break;
		case 2:kolz--; break;
		case 3:koln--; break;
		}
		rez -= kolv * kolz*(kolz - 1)*koln*(koln - 1)*(koln - 2) / 12;
		switch (num[j]) {
		case 1:kolv++; break;
		case 2:kolz++; break;
		case 3:koln++; break;
		}
		rez += kolv * kolz*(kolz - 1)*koln*(koln - 1)*(koln - 2) / 12;
		i -= 2;
		j -= 1;
	}
	cout<<rez<<endl;
	return 0;
}


#include<iostream>
using namespace std;

int num[100001];

int main()
{
	int g, d, f;
	cin>>g>>d>>f;
	for (int i = 0; i<100001; i++) num[i] = 0;
	for (int i = 0; i<g; i++) {
		int a;
		cin>>a;
		num[a] = 1;
	}
	for (int i = 0; i<d; i++)
	{
		int a;
		cin>>a;
		num[a] = 2;
	}
	for (int i = 0; i<f; i++) {
		int a;
		cin>>a;
		num[a] = 3;
	}
	int i = 100000, j = 50000, koln = 0, kolz = 0, kolv = 0, rez;
	for (int k = j; k <= i; k++)
	{
		switch (num[k]) {
		case 1:kolv++; break;
		case 2:kolz++; break;
		case 3:koln++; break;
		}
	}
	rez = kolv * kolz*(kolz - 1)*koln*(koln - 1)*(koln - 2) / 12;
	i -= 2;
	j -= 1;
	while (i - j>4) {
		switch (num[i + 2])
		{
		case 1:kolv--; break;
		case 2:kolz--; break;
		case 3:koln--; break;
		}
		switch (num[i + 1]) {
		case 1:kolv--; break;
		case 2:kolz--; break;
		case 3:koln--; break;
		}
		switch (num[j]) {
		case 1:kolv++; break;
		case 2:kolz++; break;
		case 3:koln++; break;
		}
		rez += (num[j] == 1 ? kolz * (kolz - 1)*koln*(koln - 1)*(koln - 2) / 12 : num[j] == 2 ? kolv * (kolz - 1)*(koln - 1)*koln*(koln - 2) / 6 : num[j] == 3 ? kolv * kolz*(kolz - 1)*(koln - 1)*(koln - 2) / 4 : 0);
		i -= 2;
		j -= 1;
	}
	cout<<rez<<endl;
	return 0;
}


#include<iostream>
using namespace std;

int num[100001];

int main()
{
	int g, d, f;
	cin>>g>>d>>f;
	for (int i = 0; i<100001; i++) num[i] = 0;
	for (int i = 0; i<g; i++) {
		int a;
		cin>>a;
		num[a] = 1;
	}
	for (int i = 0; i<d; i++)
	{
		int a;
		cin>>a;
		num[a] = 2;
	}
	for (int i = 0; i<f; i++) {
		int a;
		cin>>a;
		num[a] = 3;
	}
	int i = 100000, j = 50000, koln = 0, kolz = 0, kolv = 0, rez;
	for (int k = j; k <= i; k++)
	{
		switch (num[k]) {
		case 1:kolv++; break;
		case 2:kolz++; break;
		case 3:koln++; break;
		}
	}
	rez = kolv * kolz*(kolz - 1)*koln*(koln - 1)*(koln - 2) / 12;
	i -= 2;
	j -= 1;
	while (i - j>4) {
		switch (num[i + 2])
		{
		case 1:kolv--; break;
		case 2:kolz--; break;
		case 3:koln--; break;
		}
		switch (num[i + 1]) {
		case 1:kolv--; break;
		case 2:kolz--; break;
		case 3:koln--; break;
		}
		rez -= (num[j + 1] == 1 ? kolz * (kolz - 1)*koln*(koln - 1)*(koln - 2) / 12 : num[j + 1] == 2 ? kolv * (kolz - 1)*(koln - 1)*koln*(koln - 2) / 6 : num[j + 1] == 3 ? kolv * kolz*(kolz - 1)*(koln - 1)*(koln - 2) / 4 : 0);
		switch (num[j]) {
		case 1:kolv++; break;
		case 2:kolz++; break;
		case 3:koln++; break;
		}
		rez += kolv * kolz*(kolz - 1)*koln*(koln - 1)*(koln - 2) / 12;
		i -= 2;
		j -= 1;
	}
	cout<<rez<<endl;
	return 0;
}


#include<iostream>
using namespace std;

int num[100001];

int main()
{
	int g, d, f;
	cin>>g>>d>>f;
	for (int i = 0; i<100001; i++) num[i] = 0;
	for (int i = 0; i<g; i++) {
		int a;
		cin>>a;
		num[a] = 1;
	}
	for (int i = 0; i<d; i++)
	{
		int a;
		cin>>a;
		num[a] = 2;
	}
	for (int i = 0; i<f; i++) {
		int a;
		cin>>a;
		num[a] = 3;
	}
	int i = 100000, j = 50000; long long koln = 0, kolz = 0, kolv = 0, rez;
	for (int k = j; k <= i; k++)
	{
		switch (num[k]) {
		case 1:kolv++; break;
		case 2:kolz++; break;
		case 3:koln++; break;
		}
	}
	rez = kolv * kolz*(kolz - 1)*koln*(koln - 1)*(koln - 2) / 12;
	i -= 2;
	j -= 1;
	while (i - j>4) {
		switch (num[i + 2])
		{
		case 1:kolv--; break;
		case 2:kolz--; break;
		case 3:koln--; break;
		}
		switch (num[i + 1]) {
		case 1:kolv--; break;
		case 2:kolz--; break;
		case 3:koln--; break;
		}
		rez += (num[j] == 1 ? kolz * (kolz - 1)*koln*(koln - 1)*(koln - 2) / 12 : (num[j] == 2 ? kolv * kolz*(koln - 1)*koln*(koln - 2) / 6 : (num[j] == 3 ? kolv * kolz*(kolz - 1)*(koln - 1)*koln / 4 : 0)));
		switch (num[j]) {
		case 1:kolv++; break;
		case 2:kolz++; break;
		case 3:koln++; break;
		}
		i -= 2;
		j -= 1;
	}
	cout<<rez<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define int long long

typedef long long li;
typedef long double ld;

void solve();
int32_t main() {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
    #endif
    cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
const int inf = 1e16;
int g, d, f;
vector<int> lists[3];
int want[3] = {1, 2, 3};
int get_cnt (int which, int mx, int mn) {
	auto it1 = upper_bound(lists[which].begin(), lists[which].end(), mx);
	auto it2 = lower_bound(lists[which].begin(), lists[which].end(), mn);
	int leq_max = it1 - lists[which].begin();
	int l_min = it2 - lists[which].begin();
	return leq_max - l_min;
}
int choose (int a, int b) {
	if (a < b)
		return 0;
	if (b == 0)
		return 1;
	if (b == 1)
		return a;
	if (b == 2)
		return (a * (a - 1)) / 2;
	if (b == 3)
		return (a * (a - 1) * (a - 2)) / 6;
}
void solve() {
	cin >> g >> d >> f;
	lists[0].resize(g); lists[1].resize(d); lists[2].resize(f);
	for (int i = 0; i < 3; ++i) {
		for (int j = 0; j < (int) lists[i].size(); ++j) {
			cin >> lists[i][j];
		}
	}
	for (int i = 0; i < 3; ++i) {
		lists[i].push_back(inf);
		sort(lists[i].begin(), lists[i].end());
	}
	int ans = 0;
	for(int mx = 0; mx < 3; ++mx){
		for (int mn = 0; mn < 3; ++mn){
			if (mx == mn)
				continue;
			for (int i = 0; i < (int) lists[mn].size() - 1; ++i) {
				for (int j = 0; j < (int) lists[mx].size() - 1; ++j) {
					if (lists[mx][j] < lists[mn][i]) continue;
					if (lists[mx][j] > 2 * lists[mn][i]) continue;
					int remain = (0 ^ 1) ^ 2;
					remain ^= mx; remain ^= mn;
					want[mx]--;
					want[mn]--;
					int a = get_cnt(remain, lists[mx][j], lists[mn][i]);
					int b = get_cnt(mx, lists[mx][j], lists[mn][i]);
					int c = get_cnt(mn, lists[mx][j], lists[mn][i]);
					int ans_here = (choose(a, want[remain]) * choose(b - 1, want[mx]) * choose(c - 1, want[mn]));
					ans += ans_here;
					want[mx]++;
					want[mn]++;
				}
			}
		}
	}
	for (int mnmx = 1; mnmx < 3; ++mnmx) {
		for (int i = 0; i < (int) lists[mnmx].size() - 1; ++i) {
			for (int j = i + 1; j < (int) lists[mnmx].size() - 1; ++j) {
				if (lists[mnmx][j] > 2 * lists[mnmx][i])
					continue;
				want[mnmx] -= 2;
				int ans_here = 1;
				for (auto k: {0, 1, 2}) {
					int ans_here_here = get_cnt(k, lists[mnmx][j], lists[mnmx][i]);
					if (k == mnmx) ans_here_here -= 2;
					ans_here_here = choose(ans_here_here, want[k]);
					ans_here *= ans_here_here;
				}
				want[mnmx] += 2;
				ans += ans_here;
			}
		}
	}
	cout << ans;
}


#include <iostream>

using namespace std;

enum class TYPE { G = 0, D, F };

struct Cell
{
	int value;
	TYPE type;
};

void swap(Cell* a, Cell* b)
{
	Cell t = *a;
	*a = *b;
	*b = t;
}

long long factorial(int n)
{
	return (n <= 1) ? 1 : factorial(n - 1) * n;
}

long long combination(int n, int k)
{
	long long c = 1;
	for (int i = n - k + 1; i <= n; i++)
		c *= i;

	return c / factorial(k);
}

int partition(Cell arr[], int low, int high)
{
	Cell pivot = arr[high];
	int i = (low - 1);

	for (int j = low; j <= high - 1; j++)
	{
		if (arr[j].value <= pivot.value)
		{
			i++;
			swap(&arr[i], &arr[j]);
		}
	}
	swap(&arr[i + 1], &arr[high]);
	return (i + 1);
}

void quickSort(Cell arr[], int low, int high)
{
	if (low < high)
	{
		int pi = partition(arr, low, high);

		quickSort(arr, low, pi - 1);
		quickSort(arr, pi + 1, high);
	}
}

int main()
{
	// Initializing
	int g, d, f;
	cin >> g >> d >> f;
	int size = g + d + f;

	// Input
	Cell * teammates = new Cell[size];

	for (int i = 0; i < g; i++)
	{
		int c;
		cin >> c;
		teammates[i] = { c, TYPE::G };
	}

	for (int i = g; i < g + d; i++)
	{
		int c;
		cin >> c;
		teammates[i] = { c, TYPE::D };
	}

	for (int i = g + d; i < size; i++)
	{
		int c;
		cin >> c;
		teammates[i] = { c, TYPE::F };
	}

	// Program logic
	quickSort(teammates, 0, size - 1);

	long long count = 0;
	int first = 0, last = 0;
	int gc = 0, dc = 0, fc = 0;

	while (last < size && first <= last)
	{
		while (last < size && teammates[last].value <= teammates[first].value * 2)
		{
			if (teammates[last].type == TYPE::G)
				gc++;

			else if (teammates[last].type == TYPE::D)
				dc++;

			else
				fc++;

			last++;
		}

		if (gc >= 1 && dc >= 2 && fc >= 3)
		{
			if (teammates[first].type == TYPE::D)
			{
				count += gc * (dc - 1) * combination(fc, 3);
				dc--;
			}

			else if (teammates[first].type == TYPE::F)
			{
				count += gc * combination(dc, 2) * combination(fc - 1, 2);
				fc--;
			}

			else
			{
				count += combination(dc, 2) * combination(fc, 3);
				gc--;
			}
		}

		else
		{
			if (teammates[first].type == TYPE::G)
				gc--;

			else if (teammates[first].type == TYPE::D)
				dc--;

			else
				fc--;
		}

		first++;
	}

	if (last == size)
		count += gc * combination(dc, 2) * combination(fc, 3);

	// Output
	cout << count;// << endl;
	//system("pause");

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	ll g, d, f; cin >> g >> d >> f;
	vector<double>a(g), b(d), c(f);
	for (auto &it : a)
		cin >> it;
	for (auto &it : b)
		cin >> it;
	for (auto &it : c)
		cin >> it;

	sort(a.begin(), a.end());
	sort(b.begin(), b.end());
	sort(c.begin(), c.end());

	ll ans = 0;

	for (auto it : a)
	{
		double lo = (it / 2) - 0.1;
		auto xlo = lower_bound(a.begin(), a.end(), lo) - a.begin();
		auto ylo = lower_bound(b.begin(), b.end(), lo) - b.begin();
		auto zlo = lower_bound(c.begin(), c.end(), lo) - c.begin();
		auto xhi = lower_bound(a.begin(), a.end(), it) - a.begin();
		auto yhi = lower_bound(b.begin(), b.end(), it) - b.begin();
		auto zhi = lower_bound(c.begin(), c.end(), it) - c.begin();
		ll cnta = xhi - xlo;
		ll cntb = yhi - ylo;
		ll cntc = zhi - zlo;
		// cout << it << ": " << cnta << "," << cntb << "," << cntc << "\n";
		if (cntb < 2)
			continue;
		if (cntc < 3)
			continue;
		ans += (cntb * (cntb - 1) / 2) * (cntc * (cntc - 1) * (cntc - 2) / 6);
	}

	for (auto it : b)
	{
		double lo = (it / 2) - 0.1;
		auto xlo = lower_bound(a.begin(), a.end(), lo) - a.begin();
		auto ylo = lower_bound(b.begin(), b.end(), lo) - b.begin();
		auto zlo = lower_bound(c.begin(), c.end(), lo) - c.begin();
		auto xhi = lower_bound(a.begin(), a.end(), it) - a.begin();
		auto yhi = lower_bound(b.begin(), b.end(), it) - b.begin();
		auto zhi = lower_bound(c.begin(), c.end(), it) - c.begin();
		ll cnta = xhi - xlo;
		ll cntb = yhi - ylo;
		ll cntc = zhi - zlo;
		// cout << it << ": " << cnta << "," << cntb << "," << cntc << "\n";
		if (cnta < 1)
			continue;
		if (cntb < 1)
			continue;
		if (cntc < 3)
			continue;
		ans += cnta * cntb * (cntc * (cntc - 1) * (cntc - 2) / 6);
	}

	for (auto it : c)
	{
		double lo = (it / 2) - 0.1;
		auto xlo = lower_bound(a.begin(), a.end(), lo) - a.begin();
		auto ylo = lower_bound(b.begin(), b.end(), lo) - b.begin();
		auto zlo = lower_bound(c.begin(), c.end(), lo) - c.begin();
		auto xhi = lower_bound(a.begin(), a.end(), it) - a.begin();
		auto yhi = lower_bound(b.begin(), b.end(), it) - b.begin();
		auto zhi = lower_bound(c.begin(), c.end(), it) - c.begin();
		ll cnta = xhi - xlo;
		ll cntb = yhi - ylo;
		ll cntc = zhi - zlo;
		// cout << it << ": " << cnta << "," << (cntb * (cntb - 1) / 2) << "," << (cntc * (cntc - 1) / 2) << "\n";
		if (cnta < 1)
			continue;
		if (cntb < 2)
			continue;
		if (cntc < 2)
			continue;
		ans += cnta * (cntb * (cntb - 1) / 2) * (cntc * (cntc - 1) / 2);
	}

	cout << ans;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ldb ldouble
#define szz(x) (int)(x).size()
#define repi(i, p, n) for(int i=p;i<n;i++)
#define repl(i, p, n) for(ll i=p;i<n;i++)
#define Se second
#define Fi first

typedef tuple <int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;

//ll Min=1000000000000000000;
//ll Max=-Min;
ll n,k,m;
ull sum = 0;
vector <int>a;
vector <int>b;
vector <int>c;

int main()
{
    cin>>n>>m>>k;
    if(n<1 || m<2 || k<3)
    {
        cout<<0;
        return 0;
    }
    repl(i,0,n)
    {
        int te;
        cin>>te;
        a.push_back(te);
    }
    repl(i,0,m)
    {
        int te;
        cin>>te;
        b.push_back(te);
    }
    repl(i,0,k)
    {
        int te;
        cin>>te;
        c.push_back(te);
    }
    sort(all(a));
    sort(all(b));
    sort(all(c));
    ll vb1=0,vb2=0,vb3=0;
    repl(i,1,100001)
    {
        if(i==a[vb1])
        {
            ll na=0,za=0;
            for(ll j=0;j<m;j++)
            {
                if(i<b[j] && i*2+1>b[j])
                {
                    za++;
                }
            }
            for(ll j=0;j<k;j++)
            {
                if(i<c[j] && i*2+1>c[j])
                {
                    na++;
                }
            }
            //cout<<za<<" "<<na<<endl;
            sum+=(za*(za-1)/2)*(na*(na-1)*(na-2)/6);
            vb1++;
        }
        if(i==b[vb2])
        {
            ll na=0,za=0,mo=0;
            for(ll j=0;j<n;j++)
            {
                if(i<a[j] && i*2+1>a[j])
                {
                    za++;
                }
            }
            for(ll j=0;j<k;j++)
            {
                if(i<c[j] && i*2+1>c[j])
                {
                    na++;
                }
            }
            for(ll j=0;j<m;j++)
            {
                if(i<b[j] && i*2+1>b[j])
                {
                    mo++;
                }
            }
            //cout<<za<<" "<<na<<" "<<mo<<endl;
            sum+=za*(na*(na-1)*(na-2)/6)*mo;
            vb2++;
        }
        if(i==c[vb3])
        {
            ll na=0,za=0,mo=0;
            for(ll j=0;j<n;j++)
            {
                if(i<a[j] && i*2+1>a[j])
                {
                    za++;
                }
            }
            for(ll j=0;j<m;j++)
            {
                if(i<b[j] && i*2+1>b[j])
                {
                    mo++;
                }
            }
            for(ll j=0;j<k;j++)
            {
                if(i<c[j] && i*2+1>c[j])
                {
                    na++;
                }
            }
            //cout<<za<<" "<<na<<" "<<mo<<endl;
            sum+=za*(na*(na-1)/2)*(mo*(mo-1)/2);
            vb3++;
        }
    }
    cout<<sum;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ldb ldouble
#define szz(x) (int)(x).size()
#define repi(i, p, n) for(int i=p;i<n;i++)
#define repl(i, p, n) for(ll i=p;i<n;i++)
#define Se second
#define Fi first

typedef tuple <int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;

//ll Min=1000000000000000000;
//ll Max=-Min;
ll n,k,m;
ull sum = 0;
vector <int>a;
vector <int>b;
vector <int>c;

int main()
{
    cin>>n>>m>>k;
    if(n<1 || m<2 || k<3)
    {
        cout<<0;
        return 0;
    }
    repl(i,0,n)
    {
        int te;
        cin>>te;
        a.push_back(te);
    }
    repl(i,0,m)
    {
        int te;
        cin>>te;
        b.push_back(te);
    }
    repl(i,0,k)
    {
        int te;
        cin>>te;
        c.push_back(te);
    }
    repl(i,1,100001)
    {
        if(find(a.begin(), a.end(), i)!=a.end())
        {
            ll na=0,za=0;
            for(ll j=i+1;j<i*2+1;j++)
            {
                if(find(b.begin(), b.end(), j)!=b.end())
                {
                    za++;
                }
                if(find(c.begin(), c.end(), j)!=c.end())
                {
                    na++;
                }
            }
            if(za>1 && na>2)
            {
                sum+=(za*(za-1)/2)*(na*(na-1)*(na-2)/6);
            }
        }
        if(find(b.begin(), b.end(), i)!=b.end())
        {
            ll na=0,za=0,mo=0;
            for(ll j=i+1;j<i*2+1;j++)
            {
                if(find(a.begin(), a.end(), j)!=a.end())
                {
                    za++;
                }
                if(find(c.begin(), c.end(), j)!=c.end())
                {
                    na++;
                }
                if(find(b.begin(), b.end(), j)!=b.end())
                {
                    mo++;
                }
            }
            if(za>0 && na>2)
            {
                sum+=za*(na*(na-1)*(na-2)/6)*mo;
            }
        }
        if(find(c.begin(), c.end(), i)!=c.end())
        {
            ll na=0,za=0,mo=0;
            for(ll j=i+1;j<i*2+1;j++)
            {
                if(find(a.begin(), a.end(), j)!=a.end())
                {
                    za++;
                }
                if(find(b.begin(), b.end(), j)!=b.end())
                {
                    na++;
                }
                if(find(c.begin(), c.end(), j)!=c.end())
                {
                    mo++;
                }
            }
            if(za>0 && na>1)
            {
                sum+=za*(na*(na-1)/2)*(mo*(mo-1)/2);
            }
        }
    }
    cout<<sum;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ldb ldouble
#define szz(x) (int)(x).size()
#define repi(i, p, n) for(int i=p;i<n;i++)
#define repl(i, p, n) for(ll i=p;i<n;i++)
#define Se second
#define Fi first

typedef tuple <int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;

//ll Min=1000000000000000000;
//ll Max=-Min;
ll n,k,m;
ull sum = 0;
vector <ll>a;
vector <ll>b;
vector <ll>c;

int main()
{
    ll Min,Max;
    cin>>n>>m>>k;
    if(n<1 || m<2 || k<3)
    {
        cout<<0;
        return 0;
    }
    repl(i,0,n)
    {
        ll te;
        cin>>te;
        a.push_back(te);
    }
    repl(i,0,m)
    {
        ll te;
        cin>>te;
        b.push_back(te);
    }
    repl(i,0,k)
    {
        ll te;
        cin>>te;
        c.push_back(te);
    }
    vector <ll> f(6);
    repi(i,0,6)
    {
        f[i]=0;
    }
    sort(all(a));
    sort(all(b));
    sort(all(c));
    repl(i,1,100001)
    {
        if(find(a.begin(), a.end(), i)!=a.end())
        {
            ll na=0,za=0;
            for(ll j=i+1;j<i*2+1;j++)
            {
                if(find(b.begin(), b.end(), j)!=b.end())
                {
                    za++;
                }
                if(find(c.begin(), c.end(), j)!=c.end())
                {
                    na++;
                }
            }
            if(za>1 && na>2)
            {
                sum+=(za*(za-1)/2)*(na*(na-1)*(na-2)/6);
            }
        }
        if(find(b.begin(), b.end(), i)!=b.end())
        {
            ll na=0,za=0,mo=0;
            for(ll j=i+1;j<i*2+1;j++)
            {
                if(find(a.begin(), a.end(), j)!=a.end())
                {
                    za++;
                }
                if(find(c.begin(), c.end(), j)!=c.end())
                {
                    na++;
                }
                if(find(b.begin(), b.end(), j)!=b.end())
                {
                    mo++;
                }
            }
            if(za>0 && na>2)
            {
                sum+=za*(na*(na-1)*(na-2)/6)*mo;
            }
        }
        if(find(c.begin(), c.end(), i)!=c.end())
        {
            ll na=0,za=0,mo=0;
            for(ll j=i+1;j<i*2+1;j++)
            {
                if(find(a.begin(), a.end(), j)!=a.end())
                {
                    za++;
                }
                if(find(b.begin(), b.end(), j)!=b.end())
                {
                    na++;
                }
                if(find(c.begin(), c.end(), j)!=c.end())
                {
                    mo++;
                }
            }
            if(za>0 && na>1)
            {
                sum+=za*(na*(na-1)/2)*(mo*(mo-1)/2);
            }
        }
    }
    cout<<sum;
    return 0;
}


#include<bits/stdc++.h>
 using namespace std;

 #define ll long long
 #define pb push_back
 #define fi first
 #define se second
 #define FILE "rooms"
 #define ull unsigned long long
 #define ld long double
 #define rev reverse
 #define pob pop_back()
 #define sz size()
 #define bb begin()
 #define ee end()
 #define ld long double

int g,d,f;
const int maxn=1e3+228;
int a[maxn],b[maxn],c[maxn];
vector<pair<int,int> > as;
struct help
{
    int vrat;
    int zash;
    int napa;
};

help dp[maxn];

 int main()
 {
     ios_base::sync_with_stdio(false);cin.tie(0), cout.tie(0);
     /*freopen(FILE ".in", "r", stdin);
     freopen( FILE ".out", "w", stdout);*/
    cin >> g >> d >> f;
    as.pb({-1,-1});
    for(int i=0;i<g;i++)
    {
        cin >> a[i];
        as.pb({a[i],1});
    }
    for(int i=0;i<d;i++)
    {
        cin >> b[i];
        as.pb({b[i],2});
    }
    for(int i=0;i<f;i++)
    {
        cin >> c[i];
        as.pb({c[i],3});
    }
    sort(as.bb,as.ee);
    for(int i=1;i<as.sz;i++)
    {
        if(as[i].se==1)
        {
            dp[i].vrat=dp[i-1].vrat+1;
            dp[i].napa=dp[i-1].napa;
            dp[i].zash=dp[i-1].zash;
        }
        else if(as[i].se==2)
        {
            dp[i].zash=dp[i-1].zash+1;
            dp[i].napa=dp[i-1].napa;
            dp[i].vrat=dp[i-1].vrat;
        }
        else
        {
            dp[i].napa=dp[i-1].napa+1;
            dp[i].vrat=dp[i-1].vrat;
            dp[i].zash=dp[i-1].zash;
        }
    }
    ll ans1=0;
    for(int i=1;i<as.sz;i++)
    {
        int l=i;
        for(int j=i;j<as.sz;j++)
        {
            if(as[j].fi<=2*as[i].fi)
            {
                l=j;
            }
            else
            {
                break;
            }
        }
        ll v=dp[l].vrat-dp[i-1].vrat;
        ll z=dp[l].zash-dp[i-1].zash;
        ll n=dp[l].napa-dp[i-1].napa;
        if(v>=1 && z>=2 && n>=3)
        {
            ll f1,f2,f3;
            if(as[i].se==1)
            {
                f1=1;
                f2=(z*(z-1))/2;
                f3=(n*(n-1)*(n-2))/6;
            }
            else if(as[i].se==2)
            {
                f1=v;
                f2=z-1;
                f3=(n*(n-1)*(n-2))/6;
            }
            else
            {
                f1=v;
                f2=(z*(z-1))/2;
                f3=((n-1)*(n-2))/2;
            }
            ans1+=(f1*f2*f3);
        }
    }
    cout << ans1;
    return 0;
 }


#include     <algorithm>
#include         <array>
#include        <chrono>
#include         <cmath>
#include       <cstdint>
#include        <cstdio>
#include       <cstdlib>
#include       <cstring>
#include         <deque>
#include    <functional>
#include       <iomanip>
#include      <iostream>
#include      <iterator>
#include          <list>
#include           <map>
#include      <memory.h>
#include        <memory>
#include       <numeric>
#include         <queue>
#include        <random>
#include           <set>
#include       <sstream>
#include         <stack>
#include        <string>
#include <unordered_map>
#include <unordered_set>
#include        <vector>

using namespace std;

#ifndef ONLINE_JUDGE
using namespace chrono;
#define PROFBEG(wh) auto t1##wh = chrono::high_resolution_clock::now ();
#define PROFEND(wh) auto t2##wh = chrono::high_resolution_clock::now (); \
    duration<double> dur##wh = duration_cast<duration<double> > (t2##wh - t1##wh); \
    cout << endl << "time " << dur##wh.count () << endl;
#else
#define PROFBEG(wh)
#define PROFEND(wh)
#endif

#define myassert(expr) do { if (!(expr)) *((int *) nullptr) += 5; } while (false);

typedef int64_t ll;
typedef uint64_t ull;
typedef long double ld;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;

const ll posinf = 9223372036854775807LL;
const ll neginf = 9223372036854775808LL;
const ull ullinf = 18446744073709551615ULL;
// 2'305'843'009'213'693'951 (2^61 - 1)
const ull bigprime = 2305843009213693951ULL;
const ull smallprime = 2147483647ULL;
const int littleprime = 3571;
const int tinyprime = 199;
const ll modbase = 1000000007LL;
const ll inv2mod = 500000004LL;
const int sneginf = -2147483647;
const ld pi = 3.1415926535897932384626433832795;

int main ()
{
#ifndef ONLINE_JUDGE
	freopen ("input.txt", "r", stdin);
	freopen ("output.txt", "w", stdout);
#else
    //freopen ("kitchen.in", "r", stdin);
    //freopen ("kitchen.out", "w", stdout);
#endif
	cin.sync_with_stdio (false);
	cout.sync_with_stdio (false);
	cin.tie (0);

    //PROFBEG (main);

    int g, d, f; cin >> g >> d >> f;
    vector<int> gs (g), ds (d), fs (f);
    for (int &i : gs) cin >> i;
    sort (gs.begin (), gs.end ());
    for (int &i : ds) cin >> i;
    sort (ds.begin (), ds.end ());
    for (int &i : fs) cin >> i;
    sort (fs.begin (), fs.end ());

    vector<pair<int, int> > gs_par (g);
    for (int i = 0; i < g; i++)
    {
        int num = gs[i];
        gs_par[i].first = lower_bound (fs.begin (), fs.end (), (num + 1) / 2) - fs.begin ();
        gs_par[i].second = (upper_bound (fs.begin (), fs.end (), num * 2) - fs.begin ()) - 1;
    }
    vector<pair<int, int> > ds_par (d);
    for (int i = 0; i < d; i++)
    {
        int num = ds[i];
        ds_par[i].first = lower_bound (fs.begin (), fs.end (), (num + 1) / 2) - fs.begin ();
        ds_par[i].second = (upper_bound (fs.begin (), fs.end (), num * 2) - fs.begin ()) - 1;
    }
    ll ans = 0;
    auto ccf = [] (ll n) { return (n * (n - 1LL)) / 2LL; };
    for (int i = 0; i < g; i++)
    {
        if (gs_par[i].first == f)
            continue;
        int num = gs[i];
        int l = (lower_bound (ds.begin (), ds.end (), (num + 1) / 2) - ds.begin ());
        if (l == d)
            continue;
        int r = (upper_bound (ds.begin () + l, ds.end (), num * 2) - ds.begin ()) - 1;
        for (int j = l; j <= r; j++)
        {
            if (ds_par[j].first == f)
                continue;
            int lli = max (gs_par[i].first, ds_par[j].first);
            int rri = min (gs_par[i].second, ds_par[j].second);
            if (lli > rri)
                continue;
            for (int k = j + 1; k <= r; k++)
            {
                if (ds[k] > ds[j] * 2)
                    break;
                if (ds_par[k].first == f)
                    continue;
                int llii = max (lli, ds_par[k].first);
                int rrii = min (rri, ds_par[k].second);
                for (int f1 = llii; f1 <= rrii; f1++)
                {
                    int last = min ((int) (upper_bound (fs.begin () + f1, fs.end (), fs[f1] * 2) - fs.begin ()) - 1, rrii);
                    int len = last - (f1 + 1) + 1;
                    if (len < 2)
                        continue;
                    ans += ccf (ll (len));
                    /*for (int f2 = f1 + 1; f2 <= rrii; f2++)
                    {
                        if (fs[f2] > fs[f1] * 2)
                            break;
                        for (int f3 = f2 + 1; f3 <= rrii; f3++)
                        {
                            if (fs[f3] > fs[f1] * 2)
                                break;
                            ans++;
                        }
                    }*/
                }
            }
        }
    }
    cout << ans;

    //PROFEND (main);

#ifndef ONLINE_JUDGE
	//system ("pause");
#endif
	return 0;
}


#include     <algorithm>
#include         <array>
#include        <chrono>
#include         <cmath>
#include       <cstdint>
#include        <cstdio>
#include       <cstdlib>
#include       <cstring>
#include         <deque>
#include    <functional>
#include       <iomanip>
#include      <iostream>
#include      <iterator>
#include          <list>
#include           <map>
#include      <memory.h>
#include        <memory>
#include       <numeric>
#include         <queue>
#include        <random>
#include           <set>
#include       <sstream>
#include         <stack>
#include        <string>
#include <unordered_map>
#include <unordered_set>
#include        <vector>

using namespace std;

#ifndef ONLINE_JUDGE
using namespace chrono;
#define PROFBEG(wh) auto t1##wh = chrono::high_resolution_clock::now ();
#define PROFEND(wh) auto t2##wh = chrono::high_resolution_clock::now (); \
    duration<double> dur##wh = duration_cast<duration<double> > (t2##wh - t1##wh); \
    cout << endl << "time " << dur##wh.count () << endl;
#else
#define PROFBEG(wh)
#define PROFEND(wh)
#endif

#define myassert(expr) do { if (!(expr)) *((int *) nullptr) += 5; } while (false);

typedef int64_t ll;
typedef uint64_t ull;
typedef long double ld;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;

const ll posinf = 9223372036854775807LL;
const ll neginf = 9223372036854775808LL;
const ull ullinf = 18446744073709551615ULL;
// 2'305'843'009'213'693'951 (2^61 - 1)
const ull bigprime = 2305843009213693951ULL;
const ull smallprime = 2147483647ULL;
const int littleprime = 3571;
const int tinyprime = 199;
const ll modbase = 1000000007LL;
const ll inv2mod = 500000004LL;
const int sneginf = -2147483647;
const ld pi = 3.1415926535897932384626433832795;

int main ()
{
#ifndef ONLINE_JUDGE
	freopen ("input.txt", "r", stdin);
	freopen ("output.txt", "w", stdout);
#else
    //freopen ("kitchen.in", "r", stdin);
    //freopen ("kitchen.out", "w", stdout);
#endif
	cin.sync_with_stdio (false);
	cout.sync_with_stdio (false);
	cin.tie (0);

    //PROFBEG (main);

    int g, d, f; cin >> g >> d >> f;
    int n = g + d + f;
    vector<pair<int, int> > nums (n);
    for (int i = 0; i < g; i++)
    {
        cin >> nums[i].first;
        nums[i].second = 0;
    }
    for (int i = 0; i < d; i++)
    {
        cin >> nums[i + g].first;
        nums[i + g].second = 1;
    }
    for (int i = 0; i < f; i++)
    {
        cin >> nums[i + g + d].first;
        nums[i + g + d].second = 2;
    }
    sort (nums.begin (), nums.end ());
    vector<int> cnt[3];
    cnt[0].resize (n + 1);
    cnt[1].resize (n + 1);
    cnt[2].resize (n + 1);
    for (int i = 1; i <= n; i++)
    {
        cnt[0][i] = cnt[0][i - 1];
        cnt[1][i] = cnt[1][i - 1];
        cnt[2][i] = cnt[2][i - 1];
        cnt[nums[i - 1].second][i]++;
    }
    ll ans = 0;
    for (int i = 0; i < n; i++)
    {
        int mxv = (upper_bound (nums.begin () + i, nums.end (), make_pair (nums[i].first * 2, 3)) - nums.begin ()) - 1;
        ll c0 = ll (cnt[0][mxv + 1] - cnt[0][i + 1]);
        ll c1 = ll (cnt[1][mxv + 1] - cnt[1][i + 1]);
        ll c2 = ll (cnt[2][mxv + 1] - cnt[2][i + 1]);
        switch (nums[i].second)
        {
        case 0:
            ans += (c1 * (c1 - 1LL) * c2 * (c2 - 1LL) * (c2 - 2LL)) / (2LL * 6LL);
            break;
        case 1:
            ans += (c0 * c1 * c2 * (c2 - 1LL) * (c2 - 2LL)) / 6LL;
            break;
        case 2:
            ans += (c0 * c1 * (c1 - 1LL) * c2 * (c2 - 1LL)) / (2LL * 2LL);
            break;
        }
    }
    cout << ans;

    //PROFEND (main);

#ifndef ONLINE_JUDGE
	//system ("pause");
#endif
	return 0;
}


#include     <algorithm>
#include         <array>
#include        <chrono>
#include         <cmath>
#include       <cstdint>
#include        <cstdio>
#include       <cstdlib>
#include       <cstring>
#include         <deque>
#include    <functional>
#include       <iomanip>
#include      <iostream>
#include      <iterator>
#include          <list>
#include           <map>
#include      <memory.h>
#include        <memory>
#include       <numeric>
#include         <queue>
#include        <random>
#include           <set>
#include       <sstream>
#include         <stack>
#include        <string>
#include <unordered_map>
#include <unordered_set>
#include        <vector>

using namespace std;

#ifndef ONLINE_JUDGE
using namespace chrono;
#define PROFBEG(wh) auto t1##wh = chrono::high_resolution_clock::now ();
#define PROFEND(wh) auto t2##wh = chrono::high_resolution_clock::now (); \
    duration<double> dur##wh = duration_cast<duration<double> > (t2##wh - t1##wh); \
    cout << endl << "time " << dur##wh.count () << endl;
#else
#define PROFBEG(wh)
#define PROFEND(wh)
#endif

#define myassert(expr) do { if (!(expr)) *((int *) nullptr) += 5; } while (false);

typedef int64_t ll;
typedef uint64_t ull;
typedef long double ld;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;

const ll posinf = 9223372036854775807LL;
const ll neginf = 9223372036854775808LL;
const ull ullinf = 18446744073709551615ULL;
// 2'305'843'009'213'693'951 (2^61 - 1)
const ull bigprime = 2305843009213693951ULL;
const ull smallprime = 2147483647ULL;
const int littleprime = 3571;
const int tinyprime = 199;
const ll modbase = 1000000007LL;
const ll inv2mod = 500000004LL;
const int sneginf = -2147483647;
const ld pi = 3.1415926535897932384626433832795;

int main ()
{
#ifndef ONLINE_JUDGE
	freopen ("input.txt", "r", stdin);
	freopen ("output.txt", "w", stdout);
#else
    //freopen ("kitchen.in", "r", stdin);
    //freopen ("kitchen.out", "w", stdout);
#endif
	cin.sync_with_stdio (false);
	cout.sync_with_stdio (false);
	cin.tie (0);

    //PROFBEG (main);

    int g, d, f; cin >> g >> d >> f;
    vector<int> gs (g), ds (d), fs (f);
    for (int &i : gs) cin >> i;
    sort (gs.begin (), gs.end ());
    for (int &i : ds) cin >> i;
    sort (ds.begin (), ds.end ());
    for (int &i : fs) cin >> i;
    sort (fs.begin (), fs.end ());

    vector<pair<int, int> > gs_par (g);
    for (int i = 0; i < g; i++)
    {
        int num = gs[i];
        gs_par[i].first = lower_bound (fs.begin (), fs.end (), (num + 1) / 2) - fs.begin ();
        gs_par[i].second = (upper_bound (fs.begin (), fs.end (), num * 2) - fs.begin ()) - 1;
    }
    vector<pair<int, int> > ds_par (d);
    for (int i = 0; i < d; i++)
    {
        int num = ds[i];
        ds_par[i].first = lower_bound (fs.begin (), fs.end (), (num + 1) / 2) - fs.begin ();
        ds_par[i].second = (upper_bound (fs.begin (), fs.end (), num * 2) - fs.begin ()) - 1;
    }
    vector<int> fs_par (f);
    for (int i = 0; i < f; i++)
    {
        int num = fs[i];
        fs_par[i] = (upper_bound (fs.begin (), fs.end (), num * 2) - fs.begin ()) - 1;
    }
    ll ans = 0;
    auto ccf = [] (ll n) { return (n * (n - 1LL)) / 2LL; };
    for (int i = 0; i < g; i++)
    {
        if (gs_par[i].first == f)
            continue;
        int num = gs[i];
        int l = (lower_bound (ds.begin (), ds.end (), (num + 1) / 2) - ds.begin ());
        if (l == d)
            continue;
        int r = (upper_bound (ds.begin () + l, ds.end (), num * 2) - ds.begin ()) - 1;
        for (int j = l; j <= r; j++)
        {
            if (ds_par[j].first == f)
                continue;
            int lli = max (gs_par[i].first, ds_par[j].first);
            int rri = min (gs_par[i].second, ds_par[j].second);
            if (lli > rri)
                continue;
            for (int k = j + 1; k <= r; k++)
            {
                if (ds[k] > ds[j] * 2)
                    break;
                if (ds_par[k].first == f)
                    continue;
                int llii = max (lli, ds_par[k].first);
                int rrii = min (rri, ds_par[k].second);
                for (int f1 = llii; f1 <= rrii; f1++)
                {
                    int rriik = min (rrii, fs_par[f1]);
                    int len = rriik - (f1 + 1) + 1;
                    if (len < 2)
                        continue;
                    ans += ccf (ll (len));
                }
            }
        }
    }
    cout << ans;

    //PROFEND (main);

#ifndef ONLINE_JUDGE
	//system ("pause");
#endif
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

int main()
{
    long long a, b, c;
    cin >> a >> b >> c;
    vector <long long> goalKeaper(a);
    vector <long long> defender(b);
    vector <long long> attacker(c);

    vector <pair <long long, long long> > v;
    for(int i = 0; i < a; i++)
    {
        cin >> goalKeaper[i];
        v.push_back(make_pair(goalKeaper[i], 1));
    }
    for(int i = 0; i < b; i++)
    {
        cin >> defender[i];
        v.push_back(make_pair(defender[i], 2));
    }
    for(int i = 0; i < c; i++)
    {
        cin >> attacker[i];
        v.push_back(make_pair(attacker[i], 3));
    }
    sort(goalKeaper.begin(), goalKeaper.end());
    sort(defender.begin(), defender.end());
    sort(attacker.begin(), attacker.end());
    sort(v.begin(), v.end());
    //    for(int i = 0; i < v.size(); i++)
    //    cout << v[i].first << " ";
    //cout << endl;
    long long n = 0, m = 0, k = 0;
    int x = 0, y = 0;
    while(y < v.size() - 1)
    {
        if(v[y].first <= v[x].first * 2)
        {
            y++;
            //cout << v[y].first << " ";
            //cout << v[x].first << " / ";
        }
        if(v[y].first == 2 * v[x].first)
        {
            //cout << "!";
            break;
        }
        else if(v[y].first > 2 * v[x].first)
        {
            y--;
            break;
        }
    }
    //cout << "\n";
    for(int i = 0; i <= y; i++)
    {
        //cout << v[i].first << " ";
        if(v[i].second == 1)
            n++;
        else if(v[i].second == 2)
            m++;
        else
            k++;
    }
    long long counter = 0ll;
    if(n >= 1 && m >= 2 && k >= 3)
    {
        counter += n * m * (m - 1) / 2 * k * (k - 1) * (k - 2) / 6;
    }
    while(y != v.size() - 1)
    {
        y++;
        if(v[y].second == 1)
            n++;
        else if(v[y].second == 2)
            m++;
        else
            k++;

        while(v[x].first * 2 < v[y].first)
        {
            if(v[x].second == 1)
                n--;
            else if(v[x].second == 2)
                m--;
            else
                k--;
            x++;
        }
        if(n >= 1 && m >= 2 && k >= 3)
        {
            if(v[y].second == 1)
                counter += m * (m - 1) / 2 * k * (k - 1) * (k - 2) / 6;
            else if(v[y].second == 2)
                counter += n * (m - 1) * k * (k - 1) * (k - 2) / 6;
            else
                counter += n * m * (m - 1) / 2 * (k - 1) * (k - 2) / 2;
        }
    }
    cout << counter;
    return 0;
}


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MEM(a,b) memset((a),(b),sizeof(a))
const LL INF = 1e9 + 7;
const int N = 2e3 + 10;

int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int g, d, f;
	scanf("%d%d%d", &g, &d, &f);
	vector<int> a(g), b(d), c(f);
	for (auto& x : a) scanf("%d", &x);
	for (auto& x : b) scanf("%d", &x);
	for (auto& x : c) scanf("%d", &x);
	sort(a.begin(), a.end());
	sort(b.begin(), b.end());
	sort(c.begin(), c.end());
	int ans = 0;
	for (auto& x : a)
	{
		for (auto& y : b)
		{
			for (auto& z : c)
			{
				int minv = min({ x,y,z });
				if (max({ x,y,z }) > 2 * min({ x,y,z })) continue;
				int cnt1 = upper_bound(b.begin(), b.end(), 2 * minv) - upper_bound(b.begin(), b.end(), y);
				int cnt2 = upper_bound(c.begin(), c.end(), 2 * minv) - upper_bound(c.begin(), c.end(), z);
				ans += cnt1 * cnt2 * (cnt2 - 1) / 2;
			}
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MEM(a,b) memset((a),(b),sizeof(a))
const LL INF = 1e9 + 7;
const int N = 2e3 + 10;

int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int g, d, f;
	scanf("%d%d%d", &g, &d, &f);
	vector<int> a(g), b(d), c(f);
	for (auto& x : a) scanf("%d", &x);
	for (auto& x : b) scanf("%d", &x);
	for (auto& x : c) scanf("%d", &x);
	sort(a.begin(), a.end());
	sort(b.begin(), b.end());
	sort(c.begin(), c.end());
	LL ans = 0;
	for (auto& x : a)
	{
		for (auto& y : b)
		{
			for (auto& z : c)
			{
				int minv = min({ x,y,z });
				if (max({ x,y,z }) > 2 * min({ x,y,z })) continue;
				int cnt1 = upper_bound(b.begin(), b.end(), 2 * minv) - upper_bound(b.begin(), b.end(), y);
				int cnt2 = upper_bound(c.begin(), c.end(), 2 * minv) - upper_bound(c.begin(), c.end(), z);
				ans += 1LL * cnt1 * cnt2 * (cnt2 - 1) / 2;
			}
		}
	}
	printf("%lld\n", ans);
	return 0;
}


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MEM(a,b) memset((a),(b),sizeof(a))
const LL INF = 1e9 + 7;
const int N = 2e3 + 10;

int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int g, d, f;
	scanf("%d%d%d", &g, &d, &f);
	vector<int> a(g), b(d), c(f);
	for (auto& x : a) scanf("%d", &x);
	for (auto& x : b) scanf("%d", &x);
	for (auto& x : c) scanf("%d", &x);
	sort(a.begin(), a.end());
	sort(b.begin(), b.end());
	sort(c.begin(), c.end());
	LL ans = 0;
	for (int i = 0; i < a.size(); i++)
	{
		for (int j = 0; j < b.size(); j++)
		{
			int ptr1 = j;
			int ptr2 = 0;
			for (int k = 0; k < c.size(); k++)
			{
				int x = a[i], y = b[j], z = c[k];
				int minv = min({ x,y,z });
				if (max({ x,y,z }) > 2 * minv) continue;
				ptr2 = max(ptr2, k);
				while (ptr1 < b.size() && b[ptr1] <= 2 * minv) ptr1++;
				while (ptr2 < c.size() && c[ptr2] <= 2 * minv) ptr2++;
				int cnt1 = max(0, ptr1 - j - 1);
				int cnt2 = max(0, ptr2 - k - 1);
				ans += 1LL * cnt1 * cnt2 * (cnt2 - 1) / 2;
			}
		}
	}
	printf("%lld\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int a, b, c, va[1005], vb[1005], vc[1005];

int main() {
    cin >> a >> b >> c;
    for (int i = 0; i < a; ++i)
        cin >> va[i];
    for (int i = 0; i < b; ++i)
        cin >> vb[i];
    for (int i = 0; i < c; ++i)
        cin >> vc[i];
    sort(va, va + a);
    sort(vb, vb + b);
    sort(vc, vc + c);
    ll ans = 0;
    int ai = 0, bi = 0, ci = 0;
    while (ai < a && bi < b && ci < c) {
        if (va[ai] <= vb[bi] && va[ai] <= vc[ci]) {
            int upper = va[ai++] * 2;
            ll bcnt = lower_bound(vb + bi, vb + b, upper + 1) - (vb + bi);
            ll ccnt = lower_bound(vc + ci, vc + c, upper + 1) - (vc + ci);
            ans += bcnt * (bcnt - 1) / 2 * ccnt * (ccnt - 1) * (ccnt - 2) / 6;
        } else if (vb[bi] <= va[ai] && vb[bi] <= vc[ci]) {
            int upper = vb[bi++] * 2;
            ll acnt = lower_bound(va + ai, va + a, upper + 1) - (va + ai);
            ll bcnt = lower_bound(vb + bi, vb + b, upper + 1) - (vb + bi);
            ll ccnt = lower_bound(vc + ci, vc + c, upper + 1) - (vc + ci);
            ans += acnt * bcnt * ccnt * (ccnt - 1) * (ccnt - 2) / 6;
        } else {
            int upper = vc[ci++] * 2;
            ll acnt = lower_bound(va + ai, va + a, upper + 1) - (va + ai);
            ll bcnt = lower_bound(vb + bi, vb + b, upper + 1) - (vb + bi);
            ll ccnt = lower_bound(vc + ci, vc + c, upper + 1) - (vc + ci);
            ans += acnt * bcnt * (bcnt - 1) / 2 * ccnt * (ccnt - 1) / 2;
        }
    }
    cout << ans;
}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <deque>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define int int64_t
#define pb push_back
#define pbb pop_back
#define pf pop_front
#define pff push_front
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
typedef long double ld;
typedef long long ll;

const int INF = 1e9, MOD = 1e9 + 7, MAXN = 1005, MAXK = 5;
const ld EPS = 1e-9, PI = acos(-1);

int binomial[MAXN][MAXK];

void precalc(int n, int k)
{
    if (n < k) {
        binomial[n][k] = 0;
        return;
    }
    if (k == 0) {
        binomial[n][k] = 1;
        return;
    }

    int ans = 1;
    for (int i = n; i > n - k; --i)
        ans *= i;
    for (int i = 2; i <= k; ++i)
        ans /= i;
    binomial[n][k] = ans;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int g, d, f, n;
    cin >> g >> d >> f;
    n = g + d + f;
    vector<pair<int, int>> nums(n);
    for (int i = 0; i < g; ++i) {
        cin >> nums[i].first;
        nums[i].second = 0;
    }
    for (int i = g; i < g + d; ++i) {
        cin >> nums[i].first;
        nums[i].second = 1;
    }
    for (int i = g + d; i < n; ++i) {
        cin >> nums[i].first;
        nums[i].second = 2;
    }

    for (int i = 0; i <= n; ++i)
        for (int j = 0; j < MAXK; ++j)
            precalc(i, j);

    sort(all(nums));
    int answer = 0, need[] = {1, 2, 3};
    for (int left = 0; left < n; ++left) {
        int counter[3] = {0}, right = left + 1;
        need[nums[left].second]--;
        while (right < n && 2 * nums[left].first >= nums[right].first) {
            need[nums[right].second]--;

            int now = 1;
            for (int i = 0; i < 3; ++i)
                now *= (counter[i] >= 0 && need[i] >= 0 ? binomial[counter[i]][need[i]] : 0);
            answer += now;

            need[nums[right].second]++;
            counter[nums[right].second]++;
            right++;
        }
        need[nums[left].second]++;
    }

    cout << answer << endl;

    return 0;
}


#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <map>

using namespace std;
int C (int n, int k) {
    double res = 1;
    for (int i=1; i<=k; ++i)
        res = res * (n-k+i) / i;
    return (int) (res + 0.001);
}

int main(){
    int g,d,f; cin >> g >>d >>f;
    vector<vector<int>> ma(3);
    ma[0].resize(g);
    ma[1].resize(d);
    ma[2].resize(f);
    vector<pair<int, int>> all;
    for (auto& x : ma[0]) cin >> x,all.push_back({x,0});
    for (auto& x : ma[1]) cin >> x,all.push_back({x,1});
    for (auto& x : ma[2]) cin >> x,all.push_back({x,2});
    for (auto& x : ma)
        sort(x.begin(), x.end());
    long long ans=0;
    for (auto x : all){
        vector<int> qq = {0,0,0};
        for(auto y : all){

            if (x<y && 2*x.first >= y.first){
                qq[y.second]++;
            }
        }
        vector<int> mem = {1,2,3}; mem[x.second]--;
        long long lans = 1;
        for (int i = 0; i < 3; i++)
            lans*= (long long) C(qq[i],mem[i]);
        ans+=lans;

    }

    cout << ans;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long ll;

enum Type
{
	Goalkeeper, Defender, Forward
};

struct Player
{
	Type t;
	int num;
	explicit Player(Type t_, int num_) : t(t_), num(num_) { }
	bool operator <(const Player& p) const
	{
		return num < p.num;
	}
};

void read(vector<Player>& players, int k, Type t)
{
	for (int i = 0; i < k; ++i)
	{
		int num;
		cin >> num;
		players.push_back(Player(t, num));
	}
}

ll c(ll n, ll k)
{
	if (n < k)
		return 0;
	if (k == 0)
		return 1;
	if (k == 1)
		return n;
	if (k == 2)
		return n * (n - 1) / 2;
	if (k == 3)
		return n * (n - 1) * (n - 2) / 6;
	throw exception();
}

int main()
{
	vector<Player> players;
	int g, d, f;
	cin >> g >> d >> f;
	read(players, g, Goalkeeper);
	read(players, d, Defender);
	read(players, f, Forward);
	sort(players.begin(), players.end());
	ll ans = 0;
	int left = 0;
	int have[3] = { 0, 0, 0 };
	for (int i = 0; i < players.size(); ++i)
	{
		int need[3] = { 1, 2, 3 };
		need[players[i].t]--;
		have[players[i].t]--;
		for (; left < players.size() && players[left].num <= players[i].num * 2; ++left)
			have[players[left].t]++;
		ll cur = 1;
		for (int j = 0; j < 3; ++j)
			cur *= c(have[j], need[j]);
		ans += cur;
	}
	cout << ans;
	return 0;
}